[英]How to set inner HTML of a span element using jQuery
The following gives me the error Uncaught SyntaxError: Unexpected identifier :以下给我错误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?我该如何纠正这个问题?
You're not escaping the single quotes:你不是 escaping 单引号:
$('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('');
你也可以去掉第一个
$('span.xtro').html('');
, you shouldn't need it. ,你不应该需要它。
It's not the best way of using jQuery... onclick attributes are not recommended.这不是使用 jQuery 的最佳方式...不推荐使用 onclick 属性。 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编辑将 $el.bind 更改为 $el.on 这是现在使用的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.