Jquery .attr converts & to & in my links !
Am forced to use jquery and am not开发者_高级运维 familiar with it .
Anyway , i store a link in a span <span id="link">http://x.com?x=1&x=2</span>
when looking up through the js file that gets that variable and puts it in href
of a link using this code $j("#sn_title").attr('href', news_link.html());
, the &
in the link is converted to &
.
Any clues ?
&
is actually acceptable (and preferred) for links per the HTML spec.
However, if you want to make it not change what you have in news_link, you should be doing: $j("#sn_title").attr('href', news_link.text());
Notice the use of .text()
instead of .html()
The html()
method returns the actual HTML of the element.
&
is a special character in HTML (it's used for entities) and cannot appear as a normal character.
Your HTML is invalid (&x
is not an entity), but the browser automatically corrects it to &
as it is parsed.
jQuery faithfully returns the fixed-up HTML when you ask it to.
If you want the text of the element, call .text()
.
The .html() is changing the & to an html & (&) but using .attr("href") sets the link path .
Please see this link
jQuery html() and &
精彩评论