相关疑难解决方法(0)

jQuery选择并过滤div中的元素

我在选择和过滤div中的元素时遇到问题.

HTML:

<div id="wrapper">
    <input type="text" value="you can edit me">
    <input type="button" value="click me">
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery:

$("#wrapper").children().click(function() {
    alert("hi there");
});
Run Code Online (Sandbox Code Playgroud)

问题是我每次点击div内的任何内容时都会收到警报.
但我的要求是仅在用户点击按钮时发出警报.
我知道过滤jQuery中的元素正在使用:button

这是我尝试过的:

$("#wrapper").children(":button").click(function() {
    alert("hi there");
});
Run Code Online (Sandbox Code Playgroud)

$("#wrapper").children().filter(":button").click(function() {
    alert("hi there");
});
Run Code Online (Sandbox Code Playgroud)

它没用

有人知道怎么做吗?

javascript jquery filter css-selectors

26
推荐指数
1
解决办法
9万
查看次数

标签 统计

css-selectors ×1

filter ×1

javascript ×1

jquery ×1