gpt4 book ai didi

jquery - 使用 jQuery 按标题选择一个 div

转载 作者:太空宇宙 更新时间:2023-11-04 14:22:07 25 4
gpt4 key购买 nike

我有一个带有 div 的网页,其中包含几个其他没有关联 ID 的 div:

<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>

我需要能够按标题搜索其中一个 div,然后访问它的样式属性,例如 left 和 top。

我正在使用 jQuery,但我不知道如何按名称选择元素。我试过类似的东西:

var c =$('div[title~="john"]');
alert(c); //I get [object Object]
alert(c.style.left) // throws an error and is undefined

但是好像不行。

有什么帮助吗?

最佳答案

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"));

关于jquery - 使用 jQuery 按标题选择一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9152551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com