在处理事件时,如何定位具有特定类的元素
How do you target an element with a specific class when handling an event?
JQuery 新手,仍在学习...
下面的脚本在单击图像时交换div 的类。如何隔离图像,以便只有类为 .thumbnail 的图像触发脚本?
< script >
$(document).ready(function(){
$("img").click(function(e) {
var newclass = $(this).attr("class");
var oldclass = $("#fullsize").attr('class');
$("#fullsize").removeClass(oldclass).addClass(newclass);
})
});
< /script>
>$('img')
将选择文档中的所有IMG。
$('img.thumbnail')
将选择文档中具有thumbnail
类的所有 img。
您可以通过替换类属性来组合removeClass
addClass
操作。
$('img.thumbnail').click(function(e) {
var newclass = $(this).attr('id');
var oldclass = $('#fullsize').attr('class');
$('#fullsize').attr('class',newClass); // Note: No need to removeClass and then addClass. Simply Replace the value of class attribute.
});
$("img.thumbnail ").click(function(e) { // add the class along with the img selector
var newclass = $(this).attr("id");
var oldclass = $("#fullsize").attr('class');
$("#fullsize").removeClass(oldclass).addClass(newclass);
})
此链接可以帮助您了解jQuery中的不同选择器
你可以这样做
<script>
$(document).ready(function() {
$("img.thumbnail").click(function(e) {
var newclass = $(this).attr("id");
var oldclass = $("#fullsize").attr('class');
$("#fullsize").removeClass(oldclass).addClass(newclass);
});
});
</script>
相关文章:
- 在Jquery detachment()和appendTo()之后定位元素
- stickyfloat无法处理绝对定位的元素
- Selenium无法在浏览器DOM中定位元素
- 从定位目标中的元素中清除类
- 如何使用jquery对元素进行排序和定位
- 在IE9中使用jQuery.change定位select元素
- spin.js/angular spinner:如何将spin定位到DOM元素中(包括plunker演示)
- 如果元素在容器外部,是否可以相对于容器以绝对定位来定位该元素
- 根据其他调整大小的html元素定位背景图像
- 将html元素定位在页面中的任何x/y坐标处
- 相对于另一个 css 元素定位一个元素,而不编辑 html
- jquery 中的元素定位
- 我可以使用什么来生成元素定位器的名称
- 旧版浏览器中的高效元素定位
- 所见即所得,具有绝对的元素定位
- 将元素定位到另一个元素的右侧
- HTML动态/响应式元素定位
- 如何在量角器中找到元素定位器
- Css /javascript用于元素定位/大小
- Jquery元素定位问题