如何获取jQuery `$(this)`id
How to get the jQuery `$(this)` id?
如何获取触发jQuery .change()
函数的元素的id
?函数本身工作正常,但我需要为具有id="next"
的选择器执行特定操作。
$("select").change(function() {
[...snip....]
alert( $(this).attr('id') ); // <---- not working
}
你知道为什么上面的警报不起作用吗?
this
是事件挂接的DOM元素。this.id
是它的ID。无需将它包装在jQuery实例中即可获得它,id
属性在所有浏览器上都可靠地反映了该属性。
$("select").change(function() {
alert("Changed: " + this.id);
}
实例
您在代码示例中没有这样做,但如果您正在观察一个包含多个表单元素的容器,则会得到容器的ID。如果您想要触发事件的元素的ID,可以从event
对象的target
属性中获取:
$("#container").change(function(event) {
alert("Field " + event.target.id + " changed");
});
实例
(jQuery确保change
事件冒泡,即使在IE上也不会冒泡。)
您的意思是,对于id为"next"的select元素,您需要执行一些特定的脚本吗?
$("#next").change(function(){
//enter code here
});
相关文章:
- 使用this和onclick()时不能返回id值
- 当传递innerHTML和(this.id)时,循环不会终止
- 如何通过 id 获取“this”元素并查看是否选中
- 同一元素的 this.id 值在浏览器之间是不同的
- Error with this.id in Javascript
- 编写一个检查 $(this) 的开关 #id 大小写
- 如何使用 Cake PHP js helper 发送 $(this).atta('id')
- jQuery Plugin this.attr("id") undefined
- jQuery 更改 ID <大于 $(this).attr(“id”);的所有 DIV 的 ID
- 在类选择器中传递 jQuery this.id
- 使用 this.id 更改一个 ID 的 CSS
- $(this).parent().attr('id') 获取 '[object window]&
- JQuery从$(this)获取id不起作用,但获取另一个属性起作用
- 需要JS并在模块中使用this.id
- jquery中每个循环中的$(this)和$(“#id”)之间的差异
- this.id对于asp:CheckBox为空
- jquery this动画化了这个没有id的元素
- #id $(this)的子节点
- event.target.id VS event.currentTarget.id VS this.id
- JavaScript: change function(this) to function(this.id)