24

私はこの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>

いずれかの要素を押してliそのテキストを警告したいのですが、

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

どのようにお願いしますか?

4

4 に答える 4

56
$(".RestauranstSection").on('click','li',function (){
    alert($(this).text());
});
于 2013-03-18T15:56:13.247 に答える
5

liを選択して、クリックイベントに登録するだけです。

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

(また、RestaurantsSectionのスペルチェックを行うこともできます;)

于 2013-03-18T16:01:48.607 に答える
2

試す

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

デモ:フィドル

于 2013-03-18T15:55:52.040 に答える
0

コールバックにES2015矢印関数構文を使用している場合はclick、次を使用することをお勧めします$(e.currentTarget).text()

$(".RestauranstSection li").click((e)=>{ alert($(e.currentTarget).text()); });

于 2017-01-29T05:17:20.960 に答える