最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set inner HTML of a span element using jQuery - Stack Overflow

programmeradmin1浏览0评论

The following gives me the error Uncaught SyntaxError: Unexpected identifier:

$('span.xtro').html('');
$('span.xtro').html('<input type='button' class='newbutton send' value='Send request' onclick= 
                      "javascript:request('send','1','2');'>');

How can I correct this?

The following gives me the error Uncaught SyntaxError: Unexpected identifier:

$('span.xtro').html('');
$('span.xtro').html('<input type='button' class='newbutton send' value='Send request' onclick= 
                      "javascript:request('send','1','2');'>');

How can I correct this?

Share Improve this question edited Apr 8, 2012 at 15:06 Gary 13.9k18 gold badges53 silver badges72 bronze badges asked Apr 8, 2012 at 14:44 BerakiBeraki 1421 gold badge2 silver badges15 bronze badges 1
  • 2 Have a look at the syntax highlighting, and you should be able to see the problem... – Rob W Commented Apr 8, 2012 at 14:46
Add a comment  | 

2 Answers 2

Reset to default 11

You're not escaping the single quotes:

$('span.xtro').html('<input type="button" class="newbutton send" value="Send request"'
                  + ' onclick="request(\'send\',\'1\',\'2\');">');

You can also get rid of the first $('span.xtro').html('');, you shouldn't need it.

It's not the best way of using jQuery... onclick attributes are not recommended. here's an alternative

//note wrapping with double quotes and using single ones inside 
var $el = $( "<input type='button' class='newbutton send' value='Send request'>" );
$el.on( 'click', function(){ request('send','1','2'); } ); 
$('span.xtro').html('').append( $el );

EDIT changed $el.bind to $el.on which is what is used nowadays

发布评论

评论列表(0)

  1. 暂无评论