gpt4 book ai didi

javascript - 是否可以使用 jQuery.attr() 函数设置多个数据属性?

转载 作者:IT王子 更新时间:2023-10-29 02:55:04 26 4
gpt4 key购买 nike

这个有效:

$(myObj).attr("data-test-1", num1);
$(myObj).attr("data-test-2", num2);

但这不是:

$(myObj).attr({
data-test-1: num1,
data-test-2: num2
});

我是否遗漏了一些非常明显的东西?

最佳答案

当然,像这样:

$(myObj).attr({"data-test-1": num1, "data-test-2": num2});

喜欢.attr()文档状态:

Setting several attributes at once

To change the alt attribute and add the title attribute at the same time, pass both sets of names and values into the method at once using a plain JavaScript object. Each key-value pair in the object adds or modifies an attribute:

$('#greatphoto').attr({
alt: 'Beijing Brush Seller',
title: 'photo by Kelly Clark'
});

When setting multiple attributes, the quotes around attribute names are optional.

关于javascript - 是否可以使用 jQuery.attr() 函数设置多个数据属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422675/

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