为什么我不能添加< br />与JQuery .html? [英] Why can't I add a <br /> with JQuery .html?

查看:402
本文介绍了为什么我不能添加< br />与JQuery .html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这段代码可以工作:

Why does this code work:

$('div.error_container').html('<div class="error">No more foo allowed</div>');

但此代码会导致错误:

$('div.error_container').html('<div class="error">No more foo allowed<br /></div>');

我试过把< br /> 之前和之后的< / div> 标签,同样的错误。我试过将它从< br /> 更改为< br> 。 Chrome总是在检查员中显示以下内容:

I've tried putting the <br /> before and after the </div> tag, same error. I've tried changing it from <br /> to <br>. Chrome always says the following in the inspector:

Uncaught SyntaxError: Unexpected token ILLEGAL


推荐答案

尝试将其分解成一个链:

Try breaking it up into a chain:

var $div = $('<div class="error"></div>').html('No more foo allowed')
                                         .append('<br />');
$('div.error_container').html($div);

这篇关于为什么我不能添加&lt; br /&gt;与JQuery .html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆