I am trying to create a clickable link in javascript
var content = '<a href=\"#\" onclick=\"displayContent(\"TEST\")\">Whatever</a>';
$("#placeHolder").html(content);
but I keep getting an error
Uncaught SyntaxError: Unexpected token }
Is that not the correct way to escape double quotes and create a link?
I am trying to create a clickable link in javascript
var content = '<a href=\"#\" onclick=\"displayContent(\"TEST\")\">Whatever</a>';
$("#placeHolder").html(content);
but I keep getting an error
Uncaught SyntaxError: Unexpected token }
Is that not the correct way to escape double quotes and create a link?
Share Improve this question edited Jan 17, 2012 at 22:31 Alex Wayne 187k52 gold badges328 silver badges360 bronze badges asked Jan 17, 2012 at 22:26 CodeCrackCodeCrack 5,38311 gold badges47 silver badges75 bronze badges 5- 1 There's code missing here. There are no } characters in the code sample you've provided. – ceejayoz Commented Jan 17, 2012 at 22:28
- Seems the cause is out of the scope of this snippet. – Dykam Commented Jan 17, 2012 at 22:30
- You're escaping the double-quotes correctly at the very least – Sam I am says Reinstate Monica Commented Jan 17, 2012 at 22:30
- @ceejayoz, it is most likely in the context of a function, but I also think there is relevant code missing. – Dykam Commented Jan 17, 2012 at 22:31
-
As @AdamRackis' answer shows, it's the
"
around theTEST
argument. Theonclick
is going to end up looking likeonclick="displayContent("TEST")"
instead ofonclick="displayContent('TEST')"
. – user1106925 Commented Jan 17, 2012 at 22:35
4 Answers
Reset to default 7You only need to escape the single quotes
var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'
As bozdoz says:
You escape single quotes that are within single quotes; you escape double quotes that are within double quotes
But why not do
var content = $("<a />").attr("href", "#").text("Whatever").click(function(){
displayContent('TEST')
});
Or as Nathan says:
var content = $('<a href="#">Whatever</a>').click(
function() { displayContent('TEST')
});
You can avoid that mess by creating elements like this:
var content = $( "<a>", {
href: "#",
text: "whatever",
click: $.proxy( displayContent, 0, "TEST" )
});
You only need to escape quotes when they are the same type as used by your opening and closing quotes. In your example, you are unnecessarily escaping double quotes because your string is wrapped in single quotes. That being said, because of the double quotes in onclick
statement the parser will have issues with the the call to displayContent()
.
Try this, instead:
var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>';
You don't need to escape them at all!
var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>';
$("#placeHolder").html(content);
Just have it like that, as you don't need to escape "
usually, when in embeded in a '
;
But you do need to escape '
characters when it is a parameter in a function.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744350300a4569932.html
评论列表(0条)