2016-06-20 9 views
3

data-atributeを使用して進行状況バーを作成しようとしていますが、Jqueryでは両方のアクションを同時に実行できません。両方のアクションを同時に実行することはできません

<div class="progressbar"> 
    <div class="progressvalue" data-percent="50"></div> 
</div> 
.progressbar { 
    background: #9696A0; 
    height: 20px; 
    border-radius: 10px; 
    color: #fff; 
    text-align: center; 
    font-size: 14px; 
    position: relative; 
    z-index: 3; 
} 
.progressvalue { 
    background: #4650C8; 
    position: absolute; 
    top: 0; 
    bottom: 0; 
    left: 0; 
    width: 0; 
    z-index: -1; 
    border-radius: 10px; 
} 
$('.progressvalue').each(function() { 
    var $this = $(this); 
    $this.css('width', $this.data('percent') + '%'); 
    $this.parent().text('tournament progress: ' + $this.data('percent') + '%'); 
}); 

結果は次のとおりです。次の2つの問題を持っている

enter image description here

+0

問題は何ですか? https://jsfiddle.net/usyL2vcw/ – Mohammad

+0

を参照してください.Jqueryの2番目の前に "$"が欠けていたのを見ましたが、問題はテキストのみが表示されることです...テキストと進捗状況の背景が表示される必要があります。 – Liimd

+0

テキストを設定した後でDOMを調べると、色の付いた要素が削除されているように見えます。 – Douglas

答えて

3

enter image description here

しかし、それはする必要がありますs。ここには実用的な解決策があります。 https://jsfiddle.net/usyL2vcw/1/

最初の問題は$thisの代わりにthisを使用していることです。 2番目の問題は、子供がいるのdivを変更することができないことです(あなたは子供を置き換えることはできますが、子供を置き換えます)。代わりに兄弟スパンを使用します。

<div class="progressbar"> 
    <div class="progressvalue" data-percent="50"></div> 
    <span></span> 
</div> 

$('.progressvalue').each(function() { 
    var $this = $(this); 
    $this.css('width', $this.data('percent') + '%'); 
    $this.siblings().text('tournament progress: ' + $this.data('percent') + '%'); 
}); 
+0

ありがとうございました。それは今働いている。 – Liimd

関連する問題