Jquery类选择器不起作用

时间:2012-08-29 13:40:53

标签: jquery-ui jquery jquery-mobile jquery-selectors

我有以下html标签。

<span class="ui-btn-text">ALFL</span>
<span class="ui-btn-text">AL</span>

我想用

替换ALFL
<span class="ui-btn-text">A</span>

通过jquery。

我用过

$("span .ui-btn-text").replaceWith("<span class="ui-btn-text">A</span>");

但它不起作用。请给我一些答案。

提前致谢。

6 个答案:

答案 0 :(得分:1)

您的选择器匹配具有类ui-btn-text且父级为span的元素。你只需要删除空间。

我还注意到你的replaceWith用一些内容替换了自身的副本。没有必要替换元素,只需设置文本:

$("span.ui-btn-text").text("A");

答案 1 :(得分:1)

关于什么

$("span.ui-btn-text").replaceWith("<span class='ui-btn-text'>A</span>");

$("span.ui-btn-text").text("A");

答案 2 :(得分:0)

您的选择器正在.ui-btn-next元素中查找类span的元素,因为您包含了多余的空格字符:

$("span.ui-btn-text").replaceWith("<span class="ui-btn-text">A</span>");

应该是你想要的。此外,如果您只需要更改元素的内容(即“A”),请使用text()html()

$("span.ui-btn-text").text('A');

答案 3 :(得分:0)

$("span.ui-btn-text").replaceWith('<span class="ui-btn-text">A</span>');

您编写的替换字符串将导致Javascript引擎崩溃。

答案 4 :(得分:0)

如果您只想更改 ALFL 文字,可以执行以下操作:

var $span= $('span.ui-btn-text');
$span.each(function(){
    if ($(this).text() =='ALFL') {
        $(this).text('A');
    }        
});

Js小提琴演示:http://jsfiddle.net/95CyN/

如果您想使用类ui-btn-text更改所有范围的text / html,请执行以下操作:

 // change the text inside the element
 $('span.ui-btn-text').text('A');

OR

 // change the html content inside the element
 $('span.ui-btn-text').html('A');

OR

  // Replace the html element
  $('span.ui-btn-text').replaceWith('<span class="ui-btn-text">A</span>');

答案 5 :(得分:0)

Try this code friend:-

 $("span.ui-btn-text").each(function () {                   
                if ($(this).html() == "ALFL") {
                    $(this).html("A");
                }
            });
        });