开发者

Can we add any attributes to any XHTML Tag through Javascript ? for example title in <a title="text>

开发者 https://www.devze.com 2022-12-11 20:05 出处:网络
For some reason I can\'t edit XHTML source. Can we add any attributes to any XHTML Tag through Javascript, for example tit开发者_JAVA百科le in<a title=\"text\">?Yes. There is no need to break ou

For some reason I can't edit XHTML source. Can we add any attributes to any XHTML Tag through Javascript, for example tit开发者_JAVA百科le in <a title="text">?


Yes. There is no need to break out the jQuery for something so very simple. Also avoid setAttribute in general as there are problems with it in IE (not for the title attribute, but for many others).

Instead just use the perfectly normal DOM Level 1 HTML property:

link.title= 'text';

So eg. if the link you wanted to change was <a href="..." id="foo">:

document.getElementById('foo').title= 'text';    


With jQuery there's the attr() methiod that allows you to do that: http://docs.jquery.com/Attributes/attr

HTML:

<a href="#">link</a>

Javascript (jQuery):

$("a").attr("title","my title");


Short answer: yes you can.

On the question: How to do it? See documentation on setAttribute method.


Using Jquery I believe it is faily easy to do

Here is the related section in Jquery documentation


Take a look at the DOM.

http://www.javascriptkit.com/javatutors/dom2.shtml

0

精彩评论

暂无评论...
验证码 换一张
取 消