0

スパンがない場合、選択したクラスで li に箇条書きを追加しようとしています。現在のところ、リンクがクリックされるたびに箇条書きが追加されています。クリックするたびに箇条書きを追加し続けるのではなく、箇条書きを追加および削除する最良の方法は何ですか?

HTML

<ul class="files-list">
    <li class="selected">
        <span>•&lt;/span>
        "12-0001.hello-world.html"
    </li>
    <li>12-0002.hello-world.html</li>
</ul>

JS

$(this).prepend($('<span />').html('&bull;')).addClass('selected').siblings().removeClass('selected').find('span').remove();
4

2 に答える 2

0

チェック条件を追加する

var $this = $(this);
if( !$this.find('span').length){
   // Your code here
}

完全なコード

$('li').on('click', function() {
    var $this = $(this);
    if (!$this.find('span').length) {
        $this.prepend($('<span/>').html('&bull;'))
            .addClass('selected')
            .siblings()
            .removeClass('selected')
            .find('span').remove();
    }
});​

フィドルをチェック

于 2012-11-28T20:10:08.503 に答える