jquery 获取 <li> 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15481162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery get the text inside <li>
提问by Marco Dinatsoli
I have this div
我有这个 div
<div class="RestauranstSection">
<label>
Restaurant:
</label>
<input type="text"/>
<input type="button" value="Search"/>
<ul>
<?php while ($row = $restaurants->fetch()) {
?>
<li id="<?php echo $row['ID']; ?>">
<?php echo $row['name']; ?>
</li>
<?php } ?>
</ul>
</div>
I want when press on any li
elements to alert its text,
我想在按下任何li
元素时提醒其文本,
$(".RestauranstSection").on('click','li',function (){});
how please?
请问怎么样?
回答by jtepe
$(".RestauranstSection").on('click','li',function (){
alert($(this).text());
});
回答by Michael Brown
You just have to select the li and register for the click event like so
你只需要选择 li 并像这样注册点击事件
$(".RestauranstSection li").click(function (){
alert($(this).text());
});
(you also might want to spell check RestaurantsSection ;)
(您可能还想拼写检查 RestaurantSection ;)
回答by Arun P Johny
回答by ajduke
If you are using ES2015 arrow function syntax for click
callback, you may want to use $(e.currentTarget).text()
如果您使用 ES2015 箭头函数语法进行click
回调,您可能需要使用$(e.currentTarget).text()
$(".RestauranstSection li").click((e)=>{
alert($(e.currentTarget).text());
});
$(".RestauranstSection li").click((e)=>{
alert($(e.currentTarget).text());
});