jQuery:通过 .attr() 添加两个属性;方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13014317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Adding two attributes via the .attr(); method
提问by Ricardo Zea
EDIT:
编辑:
I learned that using other value than _blank
, DOES NOT work on mobile browsers to open new windows/tabs.
我了解到使用 , 以外的其他值_blank
在移动浏览器上无法打开新的窗口/选项卡。
For example, if you need to open a new window/tab:
例如,如果您需要打开一个新窗口/标签:
This workson all browsers, even mobile browsers:
target="_blank"
.This does not workon mobile browsers, but it does workon desktop browsers:
target="new"
.
这适用于所有浏览器,甚至移动浏览器:
target="_blank"
.这不适用于移动浏览器,但适用于桌面浏览器:
target="new"
.
--
——
Although I have this one working, I'm not sure if there's a better way to do it, or if the way I got it is the right/only way.
虽然我有这个工作,但我不确定是否有更好的方法来做到这一点,或者我的方法是否是正确/唯一的方法。
Basically what I'm doing is replacing all the target="_new"
or target="_blank"
attribute values to target="nw"
, this way only one new window is open and in it all other new windows will open in order to not overwhelm the user with multiple windows.
基本上我正在做的是将所有的target="_new"
ortarget="_blank"
属性值替换为target="nw"
,这样只会打开一个新窗口,并且所有其他新窗口都将在其中打开,以免用多个窗口压倒用户。
I'm also adding a "Opens in a new window" title=""
attribute.
我还添加了“在新窗口中打开”title=""
属性。
So the solution I created is this one:
所以我创建的解决方案是这样的:
$("a[target='_blank'], a[target='_new']").attr('target','nw').attr('title','Opens in a new window');
Notice the two .attr();
methods.
注意这两种.attr();
方法。
Is this the correct way to add two attributes to an element?
这是向元素添加两个属性的正确方法吗?
I tried .attr('target','nw','title','Opens in a new window')
but it didn't work.
我试过了,.attr('target','nw','title','Opens in a new window')
但没有用。
The reason I ask is because of the DYR (Don't Repeat Yourself) principle, so if I can improve the code I have, great, if not, then it is what it is.
我问的原因是因为 DYR(不要重复自己)原则,所以如果我可以改进我拥有的代码,很好,如果不能,那就是它。
Thanks.
谢谢。
回答by Adam Tomat
Should work:
应该管用:
.attr({
target:"nw",
title:"Opens in a new window",
"data-value":"internal link" // attributes which contain dash(-) should be covered in quotes.
});
Note:
注意:
" When setting multiple attributes, the quotes around attribute names are optional.
WARNING: When setting the 'class' attribute, you must always use quotes!
"设置多个属性时,属性名称周围的引号是可选的。
警告:设置 'class' 属性时,必须始终使用引号!
From the jQuery documentation (Sep 2016) for .attr:
来自.attr的 jQuery 文档(2016 年 9 月):
Attempting to change the type attribute on an input or button element created via document.createElement() will throw an exception on Internet Explorer 8 or older.
尝试更改通过 document.createElement() 创建的输入或按钮元素的 type 属性将在 Internet Explorer 8 或更早版本上引发异常。
Edit:
For future reference...
To get a singleattribute you would use
编辑:
供将来参考...要获得您将使用的单个属性
var strAttribute = $(".something").attr("title");
To set a singleattribute you would use
要设置您将使用的单个属性
$(".something").attr("title","Test");
To set multipleattributes you need to wrap everything in { ... }
要设置多个属性,您需要将所有内容包装在 { ... }
$(".something").attr( { title:"Test", alt:"Test2" } );
Edit - If you're trying to get/set the 'checked' attribute from a checkbox...
编辑 - 如果您尝试从复选框中获取/设置“已检查”属性...
You will need to use prop()
as of jQuery 1.6+
您将需要prop()
从 jQuery 1.6+ 开始使用
the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
...the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does
.prop() 方法提供了一种显式检索属性值的方法,而 .attr() 方法检索属性。
...关于checked 属性要记住的最重要的概念是它不对应于checked 属性。该属性实际上对应于 defaultChecked 属性,应该仅用于设置复选框的初始值。选中的属性值不会随着复选框的状态而改变,而选中的属性会
So to get the checked statusof a checkbox, you should use:
因此,要获取复选框的选中状态,您应该使用:
$('#checkbox1').prop('checked'); // Returns true/false
Or to set the checkbox as checked or uncheckedyou should use:
或者要将复选框设置为选中或未选中,您应该使用:
$('#checkbox1').prop('checked', true); // To check it
$('#checkbox1').prop('checked', false); // To uncheck it
回答by Derek
the proper way is:
正确的方法是:
.attr({target:'nw', title:'Opens in a new window'})
回答by Tejas Soni
If you what to add bootstrap attributes in anchor tag dynamically than this will helps you lot
如果您要动态地在锚标记中添加引导属性,那么这将对您有很大帮助
$(".dropdown a").attr({
class: "dropdown-toggle",
'data-toggle': "dropdown",
role: "button",
'aria-haspopup': "true",
'aria-expanded': "true"
});
回答by Jaydeep Patel
Something like this:
像这样的东西:
$(myObj).attr({"data-test-1": num1, "data-test-2": num2});
回答by javaprodigy
Use curly brackets and put all the attributes you want to add inside
使用大括号并将所有要添加的属性放在里面
Example:
例子:
$('#objId').attr({
target: 'nw',
title: 'Opens in a new window'
});
回答by wonbin
Multiple Attribute
var tag = "tag name";
createNode(tag, target, attribute);
createNode: function(tag, target, attribute){
var tag = jQuery("<" + tag + ">");
jQuery.each(attribute, function(i,v){
tag.attr(v);
});
target.append(tag);
tag.appendTo(target);
}
var attribute = [
{"data-level": "3"},
];