是否期望jQuery $('span')。html()将XHTML br标签转换为html语法? [英] Is it expected that jQuery $('span').html() turns XHTML br tag to html syntax?

查看:120
本文介绍了是否期望jQuery $('span')。html()将XHTML br标签转换为html语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



If I have a span tag like this:

 <span>Hi<br />SecondLine</span>

我称之为jQuery函数:

And I call this jQuery function:

 var html = $('span').html();

html设置为:

html is set to:

 Hi<br>SecondLine

现在,我意识到函数是html()而不是xhtml()。但是,这是预期的行为?我可以指望它吗,或者我总是需要检查一个xhtml br标签和一个html br标签,例如:

Now, I realize the name of the function is html() and not xhtml(). But is this expected behavior? Can I count on it, or do I alway need to check for a xhtml br tag and an html br tag, in say, this example:

function br2nl(text) {
    return text.replace(/<br \/>/gi, '\n').replace(/<br>/gi, '\n');
}


推荐答案

它只是修改浏览器DOM。

It doesn't. It just modifies the browser DOM.

使用innerHTML时,浏览器需要将DOM序列化为HTML或XHTML。不同的浏览器的行为不同。

It is up to the browser to serialise the DOM to HTML or XHTML as it desires when using innerHTML. Different browsers act differently.

这篇关于是否期望jQuery $('span')。html()将XHTML br标签转换为html语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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