jquery获取<li>中的文本

Mar*_*oli 21 html javascript jquery

我有这个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>
Run Code Online (Sandbox Code Playgroud)

我想按下任何li元素来提醒其文字,

$(".RestauranstSection").on('click','li',function (){});
Run Code Online (Sandbox Code Playgroud)

怎么样?

jte*_*epe 48

$(".RestauranstSection").on('click','li',function (){
    alert($(this).text());
});
Run Code Online (Sandbox Code Playgroud)


Mic*_*own 5

您只需要选择li并注册click事件,就像这样

$(".RestauranstSection li").click(function (){
    alert($(this).text());
});
Run Code Online (Sandbox Code Playgroud)

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