HTML JQUERY 通过span内容获得span的id

HTML JQUERY 通过span内容获得span的id,第1张

$('span')click(function(){

alert($(this)parent()prop('id')); //d出你点击的数字对应li的id

})

1、获取第一个子元素有如下方法:

(1):first

举:$('divone:first'),获取所有class="one"的div的第一个div子元素。这里的限制在于父子元素都是同一种,即父元素如果是span标签,获取的第一个子元素也是span标签,如果父元素是table标签,获取的子元素也是table标签。

(2):first-child

如:$('divone:first-child'),获取所有class="one"的div的第一个子元素,注意是“所有”,意思是说,如果这个div有多外,那么获取的第一个子元素也是有多个。

(3)使用eq()方法

如:$('divone > span:eq(1)'),获取class="one"下的div的第一个span子元素,同(2)一样,也是有多个div也是获取多个div的第一个元素。

可以通过获取id,或者直接在父层限制一个eq(1)这样的形式去获取惟一一个子元素

$("div")find("span") 是一个动作

$("div span") 是一个选择器如果没有特殊使用的话,两个都是一个意思。但是有些时候,比如在AJAX请求响应的回调过程中,我们会经常用到find()这个寻找动作,例

$ajax({

//

success:function(data){

$(data)find('#id1')

}

});

你好!

通过jquery的层级选择器,可以轻松搞定:

$('div')click(function(){

    //  ">span:nth-child(3)"    表示div的子span元素集合中的第3个元素

    //  "$(">span:nth-child(3)",this)"   表示在当前div查找

   alert(   $(">span:nth-child(3)",this)html()   );    

   

   //另外eq()方法也同样可以,注意索引从0开始

   alert(   $(">span",this)eq(2)html()   );

   

   //jquery中的children

   alert(  $(this)children("span")last()text()  );

   //匹配最后一个子元素

   alert(  $(">span:last-child",this)html()   );

   alert(  $(this)children("span:last")html()  );

   

}

希望对你有帮助!

将这个js(jquery-getHtml-valuejs)文件引入到你的页面中,将$("#ImageButton1")html()替换成$("#ImageButton1")formhtml (),具体代码:如下

(function($) {

var oldHTML = $fnhtml;

$fnformhtml =function() {

if (argumentslength) return oldHTMLapply(this,arguments);

$("input,textarea,button", this)each(function() {

thissetAttribute('value',thisvalue);

});

$(":radio,:checkbox", this)each(function() {

if (thischecked) thissetAttribute('checked', 'checked');

else thisremoveAttribute('checked');

});

$("option", this)each(function() {

if (thisselected) thissetAttribute('selected', 'selected');

else thisremoveAttribute('selected');

});

return oldHTMLapply(this);

};

})(jQuery);

以上就是关于HTML JQUERY 通过span内容获得span的id全部的内容,包括:HTML JQUERY 通过span内容获得span的id、jquery如何获取第一个或最后一个子元素、jquery 里 $("div").find("span") 和 $("div span") 区别是什么等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9497192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存