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

后端 未结 5 867
庸人自扰
庸人自扰 2021-01-27 19:57

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:

相关标签:
5条回答
  • As Aito says, you need double quotes:

    $('#results').html("<div class='tweet'><a href=\"javascript:sendDirectMessage('1711838', 'abc')\">DM</a><hr/></div>");
    
    0 讨论(0)
  • 2021-01-27 20:43

    Perhaps you should enclose with double quotes:

    <a href=\"javascript:sendDirectMessage('1711838', 'abc')\">
    
    0 讨论(0)
  • 2021-01-27 20:43
    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
    0 讨论(0)
  • 2021-01-27 20:43

    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.

    0 讨论(0)
  • 2021-01-27 20:45

    Try this:

    $('#results').html('<div class="tweet"><a href="javascript:sendDirectMessage(\'1711838\', \'abc\')">DM</a><hr/></div>');
    
    0 讨论(0)
提交回复
热议问题