【问题标题】:jQuery `find(selector)`: is there a way to reference each matched element?jQuery`find(selector)`:有没有办法引用每个匹配的元素?
【发布时间】:2013-09-09 14:45:22
【问题描述】:

我正在使用find(sel) 更改与sel 匹配的所有元素的css 属性:如果该元素设置了position,那么它应该得到position,否则它应该得到relative。

$("#mydiv").find("form").css({ "z-index": 2, "position": jQuery(this).css("position") || "relative", "background-color": "green" })

不工作.. 最好的方法来完成这个?

【问题讨论】:

  • 默认不都是static吗?他们会有一个位置值,所以也许你应该检查它是否是静态的。
  • 是的,return (jQuery(this).css("position") != "static") ? jQuery(this).css("position") : "relative"

标签: jquery reference find element


【解决方案1】:

试试这个.css()的变体

$("#mydiv").find("form").css({
    "z-index": 2,
    "position": function(){
        return jQuery(this).css("position") || "relative"
    },
    "background-color": "green"
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2015-04-22
    • 2018-02-17
    • 2012-09-15
    • 2018-10-08
    相关资源
    最近更新 更多