如何在不给他们id的情况下获得li的文本



我在ul标签中有四个li标签。

当我选择一个li并且我想隐藏其他三个li值时,我想在li标签内获得文本。

只有选中的才会显示在页面上。

<ul id="names">
  <li>a</li>
  <li>b</li>
  <li>c</li>
  <li>d</li>
</ul>
$('#names li').click(function(){
    $(this).siblings().hide();
});

这回答了你的问题的两个部分:

$('#names li').click(function(){    
  var x = $(this).text(); // gets the text of the selected li
  alert(x); // displays the text inside the selected li 
  $(this).siblings().hide();​​​​ // hides all other li's   
});

这里是一个工作的jsFiddle为您的例子。

这个怎么样?它将隐藏它们并显示使用jQuery选择器点击的那个。

$('#names li').click(function() {
    $('#names li').hide();
    $(this).show();
})

使用示例:http://jsfiddle.net/eqUD3/

试一下

$("#names li").click(function(){
   var selected = $(this);
   alert('You have selected '+ $(this).text());
   $("#names li").hide();
   $(this).show();
});

检查小提琴DEMO

相关内容

最新更新