Javascript jquery获取内部文本<;李>;

Javascript jquery获取内部文本<;李>;,javascript,jquery,html,Javascript,Jquery,Html,我有这个部门 <div class="RestauranstSection"> <label> Restaurant: </label> <input type="text"/> <input type="button" value="Search"/> <ul> <?php while ($row = $restaurants->fetch

我有这个部门

<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>
怎么办?

试试看

$(".RestauranstSection").on('click','li',function (){
    alert($(this).html())
});

演示:

您只需选择li并注册点击事件,如下所示

$(".RestauranstSection li").click(function (){
    alert($(this).text());
});

(您可能还需要拼写检查RestaurantsSection;)

如果您使用ES2015箭头函数语法进行
单击
回调,您可能需要使用
$(e.currentTarget).text()


$(“.RestauranstSection li”)。单击((e)=>{
警报($(e.currentTarget).text());
});

你读了吗?@FrédéricHamidi不,我不想成为那个家伙,但说真的,这会带来第一个结果,以及其他相关的SO问题。@FelixKling谢谢你,非常感谢,我会在10点后接受你的回答mins@MarcoDinatsoli:作为参考,
html()
将显示html标记。如果您的列表是:你好,世界,
html()
的结果将是
Hello,world!”
text()
将是
“Hello,world!”
$(".RestauranstSection").on('click','li',function (){
    alert($(this).text());
});
$(".RestauranstSection li").click(function (){
    alert($(this).text());
});