作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为什么我抓不到这个<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'> </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/
我是一名优秀的程序员,十分优秀!