用jQuery按标题选择div [英] Select a div by title with jQuery
本文介绍了用jQuery按标题选择div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< div id我有一个带有div的网页,其中包含其他几个div,但没有与它们关联的ID。 = 容器 >
< div title =jhonstyle =position:absolute; left:821px; top:778.44px; width:8px; height:9px; z-index:3;> < / DIV>
< div title =carlstyle =position:absolute; left:561px; top:579.88px; width:8px; height:9px; z-index:3;> < / DIV>
< / div>
我需要能够按标题搜索其中一个div,然后访问其样式属性,例如左和顶。
我正在使用jQuery,但我不知道如何按名称选择元素。我试过类似的东西:
var c = $('div [title〜=john]');
alert(c); //我得到[object Object]
alert(c.style.left)//抛出错误并且未定义
但似乎不起作用。
有什么帮助吗?
alert($(div [title = \john\])。css(left));
alert($(div [title = \john \])。css(top));
或者,如果名称是一个变量:
var name =john;
alert($(div [title = \+ name +\])。css(left));
alert($(div [title = \+ name +\])。css(top));
I have a webpage with a div which contains several other divs WITHOUT an ID associated to them:
<div id="container">
<div title ="jhon" style=" position:absolute; left: 821px; top: 778.44px; width:8px; height:9px; z-index:3;"> </div>
<div title ="carl" style=" position:absolute; left: 561px; top: 579.88px; width:8px; height:9px; z-index:3;"> </div>
</div>
I need to be able to search one of these div by title and then access its style attributes such as left and top.
I am using jQuery but I have no idea on how to select an element by name. I've tried something like:
var c =$('div[title~="john"]');
alert(c); //I get [object Object]
alert(c.style.left) // throws an error and is undefined
but it seems not to work.
Any help?
解决方案
alert( $("div[title=\"john\"]").css("left") );
alert( $("div[title=\"john\"]").css("top") );
or, if the name is a variable:
var name = "john";
alert($("div[title=\""+name+"\"]").css("left"));
alert($("div[title=\""+name+"\"]").css("top"));
这篇关于用jQuery按标题选择div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文