2012-08-29 87 views
0

我有以下的html标签。Jquery类选择器不工作

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

我想通过与jQuery的

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

更换ALFL。

我用

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

但它无法正常工作。请给我一些答案。

在此先感谢。

+0

加入我的答案。 – insomiac

回答

0

如果你只是想改变ALFL文字,你可以做这样的事情:

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

的js拨弄演示:所有跨度http://jsfiddle.net/95CyN/

如果你想改变的text/html带班UI的BTN-文字,这样做:

// 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>'); 
+0

非常感谢您的回答。它非常有帮助。 – Mahesh

1

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

我也注意到你的replaceWith用一些内容替换了自身的副本。有没有需要更换的部件,只需设置文本:

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

你的选择是寻找与类的.ui-btn-next一个span元素中的元素,因为你includied一个多余的空格字符:

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

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

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

关于什么的

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

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

你写的替换字符串会使Jav ascript引擎崩溃。

0
Try this code friend:- 

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