我知道这听起来有点像初学者的问题但是想象我有以下示例
<div class = "image">content 1</div>
<div class = "image">content 2</div>
<div class = "image">content 3</div>
我想使用jQuery来获取一个值的数组,例如在Vanilla JavaScript中,我们将使用
let divs = document.getElementsByClassName("image");
但是,如果我尝试使用var img = $('.image').val();
,它将仅返回最后一个值,我还检查了jQuery的一般属性页面,我找不到任何地方。
谢谢
使用每个
回答var image = {};
$(".image").each(function(index) {
image[index] = $(this).val();
});