javascript - $ (".class").width($(this)...) 不起作用

标签 javascript jquery

我有很多 div ,比如说 .class 类。我需要根据特定路线为它们分配宽度

$(".class").width($(this).parent().next().find("code").width())

但是,由于某种原因,它不适用于 $(this) 但当我这样做时它可以:

$(".class").width($(".class").parent().next().find("code").width())

但显然只适用于第一个。有什么线索吗?

最佳答案

jQuery width()方法接受一个函数:

A function returning the width to set. Receives the index position of the element in the set and the old width as arguments. Within the function, this refers to the current element in the set.

$(".class").width(function () {
    return $(this).parent().next().find("code").width()
});

关于javascript - $ (".class").width($(this)...) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235348/

相关文章:

javascript - 独立对父/子元素执行操作

javascript - 通过 ajax 请求观察 div 变化

javascript - 无法从扩展标点符号中读取元素的 innerText 的未定义属性

jquery - 使用 jQuery 循环遍历 MVC3 WebGrid 中的行

jquery - 在搜索框外显示 select2 多项选择

javascript - 是否可以将脚本 src 放入 function.php wordpress 上?

javascript - Google Maps API v3 添加带有自定义文本的信息窗口的多个标记

javascript - 如何使用angularjs计算两个纬度和经度之间的距离?

Javascript - 循环访问元素的递归函数

Jquery 对话框和 Div 位置