开发者

Issue while setting html of div using the jquery html() method

开发者 https://www.devze.com 2022-12-18 07:20 出处:网络
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.

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')\">


  1. 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.
  2. You are using JavaScript: pseudo URIs
  3. You are using invalid XHTML
  4. 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>");
0

精彩评论

暂无评论...
验证码 换一张
取 消