Ticket #4448 (closed bug: invalid)
$.getJSON() and append() link tags <a href>demo</a> sometimes is wrong
| Reported by: | opengloves | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.4 |
| Component: | data | Version: | 1.3.2 |
| Keywords: | json and appden link tag | Cc: | |
| Blocking: | Blocked by: |
Description
Good day! I use jquery-1.3.2.min.js. I want append some link tags in a div, but something is wrong, against my expect. some links are right, like this: <a href=" http://www.demo.com/123">demo</a> but some are: <a href=" http://www.demo.com/123"/>demo and these wrong tags was different is Firefox and IE 7. For example such function:
json data:[{"linkKey":"http::www.demo.com/123","linkValue":"demo"}]
html: <div id="site"></div> <div><a id="home">show</a></div>
$("#home").click(function(){
$.getJSON("demo.jsp",
function(json){
$.each(json,function(i){ $("#site").append("<span><a href="+json[i].linkKey+">"+json[i].linkValue+"</a></span>");});});});
To fix this, I have to change the function like this: function(json){
$.each(json,function(i){
n = i+1; $("site").append("<span><a id=id"+n+">"+json[i].linkValue+"</a></span>"); $("#id"+n).attr("href",json[i].linkKey);
});}
but this isn't suitable, I think.
Change History
comment:1 Changed 4 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
comment:2 Changed 4 years ago by opengloves
Thank you for your response. I quoted href attribute like this:
$("#site").append("<span><a href=\""+json[i].linkKey+"\">"+json[i].linkValue+"</a></span>");
and it works right. thank you very much.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Your href attribute is not quoted in the HTML you generate, so it is being mis-parsed in some cases. Nothing jQuery can do about invalid HTML...