Can we add any attributes to any XHTML Tag through Javascript ? for example title in <a title="text>
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
精彩评论