简体   繁体   English

如何使用jQuery添加HTML属性

[英]How to add an HTML attribute with jQuery

Well, I have this jQuery image slideshow that uses the attribute "control" inside an <a>. 好吧,我有这个jQuery图像幻灯片,它使用<a>中的属性“control”。 Seeing how it didn't validate I searched for a way to add this attribute inside my HMTL via jQuery but I didn't really find anything relevant. 看到它没有验证我搜索了一种通过jQuery在我的HMTL中添加这个属性的方法,但我没有找到任何相关的东西。 Now I don't really care about how valid my page is, but I'm really curious in how to add an HTML attribute inside an HTML tag. 现在我并不关心我的页面有多有效,但我真的很好奇如何在HTML标记内添加HTML属性。

In case I wasn't clear enough with my explanation, I have this code: 如果我对我的解释不够清楚,我有这个代码:

<a id="previous" control="-6" href="#"></a>

And I want to add control="-6" with jQuery. 我想用jQuery添加control =“ - 6”

Use jQuery's attr function 使用jQuery的attr函数

$("#previous").attr("control", "-6");

An example 一个例子

// Try to retrieve the control attribute
// returns undefined because the attribute doesn't exists
$("#previous").attr("control");

// Set the control attribute
$("#previous").attr("control", "-6");

// Retrieve the control attribute (-6)
$("#previous").attr("control");

See this example on jsFiddle 在jsFiddle上查看此示例


You can alternatively use data function to store values on elements. 您也可以使用data函数在元素上存储值。 Works the same way, for example: 以相同的方式工作,例如:

$("#previous").data("control"); // undefined
$("#previous").data("control", "-6"); // set the element data
$("#previous").data("control"); // retrieve -6

Using data you can store more complex values like objects 使用data可以存储更复杂的值,如对象

$("#previos").data("control", { value: -6 });
($("#previos").data("control")).value; // -6

See a data example on jsFiddle 请参阅jsFiddle上数据示例

Since the jQuery version has been well covered here, I thought I'd offer something different , so here a native DOM API alternative: 由于这里已经很好地介绍了jQuery版本,我认为我会提供不同的东西 ,所以这里有一个原生DOM API替代方案:

document.getElementById('previous').setAttribute('control','-6');

Yes, I know you asked for jQuery. 是的,我知道你要求jQuery。 Never hurts to know the native way too. 永远不要害怕知道本土的方式。 :o) :O)

Let me see if I understood you. 让我看看我是否理解你。 You have, for example, the code: 例如,您有代码:

<a id="previous" href="#"></a>

And by jQuery you want it to be: 通过jQuery,你希望它是:

<a id="previous" control="-6" href="#"></a>

Is it right? 这样对吗? If it is. 如果是。 You just have to do: 你只需要这样做:

$('#previous').attr('control', -6);

If an attribute doesn't exists it's created by jQuery. 如果属性不存在,则由jQuery创建。 So, to remove it you can do: 因此,要删除它,您可以执行以下操作:

$('#previous').removeAttr('control');

What you're doing doesn't respect the html rules and everything else, but it works fine, a lot of plugins do the same. 你正在做什么不尊重HTML规则和其他一切,但它工作正常,很多插件都做同样的事情。 ;D ; d

I hope this could be helpful! 我希望这可能会有所帮助!

See you! 再见!

Try this: 试试这个:

$('#previous').attr('control', '-6');

Why? 为什么? the $.attr(); $ .attr(); function of jQuery allows you to add, get or update attributes (class, id, href, link, src, control and everything else!). jQuery的功能允许你添加,获取或更新属性(class,id,href,link,src,control等等!)。

$("#previous").attr("control", "-6");

HTML: HTML:

<a id="previous" href="#">...</a> 

jQuery: jQuery的:

$('#previous').attr('control', '-6');

jQuery's attr will do that. jQuery的attr会做到这一点。 Example: 例:

$("#previous").attr("control", "-6");

Also check out this example at http://jsfiddle.net/grfSN/ . 另请参阅http://jsfiddle.net/grfSN/上的此示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM