I've got an issue with jquery where I set the html of a div using the html method, but it does not set it correctly.
Here's the stripped down code I'm using:
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
<script type='text/javascript'>
jQuery(document).ready(function($) {
$('#results').html("<div class='tweet'><a href=javascript:sendDirectMessage('1711838', 'abc')>DM</a><hr/></div>");
})
</scri开发者_开发问答pt>
</head>
<body>
<div id="results"/>
</body>
</html>
but when I view the result in the generated page using firebug, the element has it's contents set to:
<a )="" abc="" href="javascript:sendDirectMessage('1711838',">DM</a>
What am I doing wrong??
Perhaps you should enclose with double quotes:
<a href=\"javascript:sendDirectMessage('1711838', 'abc')\">
- The HTML you are trying to generate is invalid (this is your primary problem). Since you are using XHTML, the rules for quoting attributes are simple: attribute values must be quoted. Yours are not.
- You are using JavaScript: pseudo URIs
- You are using invalid XHTML
- You are not using HTML-Compatible XHTML
Try this:
$('#results').html('<div class="tweet"><a href="javascript:sendDirectMessage(\'1711838\', \'abc\')">DM</a><hr/></div>');
You forgot to quote your href
attribute.
Therefore, it stopped parsing the href
's value after the first space.
You need to write the following:
$('#results').html(
"<div class='tweet'><a href=\"javascript:sendDirectMessage('1711838', 'abc')\">DM</a><hr/></div>"
);
The \"
creates a "
inside a double-quoted string.
As Aito says, you need double quotes:
$('#results').html("<div class='tweet'><a href=\"javascript:sendDirectMessage('1711838', 'abc')\">DM</a><hr/></div>");
精彩评论