如何使用jQuery添加HTML属性 [英] How to add an HTML attribute with jQuery

查看:84
本文介绍了如何使用jQuery添加HTML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个在< a>内部使用属性control的jQuery图像幻灯片。看到它没有验证我搜索了一种方法来通过jQuery在我的HMTL中添加这个属性,但是我没有找到任何相关的东西。现在我并不关心页面的有效性,但我很好奇如何在HTML标签内添加HTML属性。



如果我我的解释不够清楚,我有这样的代码:

 < a id =previouscontrol = - 6href =#>< / a> 

我想用jQuery添加 control = - 6

解决方案

由于jQuery版本在这里覆盖得很好,我以为我会提供 >不同的东西,所以这里有一个本地的DOM API替代方案:

  document.getElementById('previous')。setAttribute ( '控制', ' -  6'); 

是的,我知道您要求提供jQuery。永远不会伤害到自己的方式。 :o)

Well, I have this jQuery image slideshow that uses the attribute "control" inside an <a>. 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. 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.

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.

解决方案

Since the jQuery version has been well covered here, I thought I'd offer something different, so here a native DOM API alternative:

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

Yes, I know you asked for jQuery. Never hurts to know the native way too. :o)

这篇关于如何使用jQuery添加HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆