gpt4 book ai didi

javascript - 无法在 jQuery 中使用 .click 捕获

转载 作者:太空狗 更新时间:2023-10-29 15:01:25 24 4
gpt4 key购买 nike

为什么我抓不到这个<a> ...

<a href="javascript:void(0)" class="select2-choice select2-default" tabindex="-1">
<span class="select2-chosen">select item</span>
<abbr class="select2-search-choice-close"></abbr>
<span class="select2-arrow">
<b></b>
</span>
</a>

...用这个 jQuery?

$(document).ready(function() {
$( ".select2-choice" ).click(function() {
alert( "Handler for .keydown() called." );
});
});

我认为这是生成下拉列表的函数:

        createContainer: function () {
var container = $(document.createElement("div")).attr({
"class": "select2-container"
}).html([
"<a href='javascript:void(0)' onclick='return false;' class='select2-choice' tabindex='-1'>",
" <span class='select2-chosen'>&nbsp;</span><abbr class='select2-search-choice-close'></abbr>",
" <span class='select2-arrow'><b></b></span>",
"</a>",
"<input class='select2-focusser select2-offscreen' type='text'/>",
"<div class='select2-drop select2-display-none'>",
" <div class='select2-search'>",
" <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input'/>",
" </div>",
" <ul class='select2-results'>",
" </ul>",
"</div>"].join(""));
return container;
}

实际上,我正在尝试解决这个问题adding button on a select2 通过在用户单击该字段时添加按钮,因为我以前的解决方案不起作用。

我在 64 位 Windows 8 机器上使用 Firefox 26.0 和 Chrome 版本 31.0.1650.63 m。

是否 <a href="javascript:void(0)" ... >有什么关系吗?感谢您的帮助。

最佳答案

因为 $( ".select2-choice") 在执行的时候没有匹配到任何东西,所以没有绑定(bind)任何事件。尝试

$(document).ready(function() {
$(document).on("click", ".select2-choice", function() { ... });
});

在执行函数之前向选择器过滤的文档添加点击事件。

关于javascript - 无法在 jQuery 中使用 .click 捕获 <a>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20983958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com