innerHTML从图像标记中删除关闭斜杠 [英] innerHTML removing closing slash from image tag

查看:227
本文介绍了innerHTML从图像标记中删除关闭斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很奇怪,有人能帮帮我吗。这是一个快速测试函数:

  function test_function(){
code ='< img src =http ://www.myimage.com/img.jpgalt =image/>';
alert(代码);
document.getElementById('test')。innerHTML = code;
alert(document.getElementById('test')。innerHTML);
}

运行上面的代码将显示 /> 在第一个警报中,但第二个警报没有,它只显示> 。所以看起来应用于 .innerHTML 去掉正斜杠。任何想法如何阻止这种情况发生?我需要正斜杠进行验证。

解决方案

第二个警报只显示浏览器用作图像内部表示的内容元件。您不需要使用斜杠进行验证,因为验证始终与您的页面源有关,验证程序不会执行动态添加元素到DOM的JavaScript。



实际上,大多数浏览器内部处理XHTML与HTML相同,而不是文档的XML表示。只有当您使用MIME类型 application / xhtml + xml 发送XHTML文档时,某些浏览器才会使用XML解析器呈现您的页面。



另见Ian Hickson的文章


Very odd, can someone help me. Here is a quick test function:

function test_function(){
    code = '<img src="http://www.myimage.com/img.jpg" alt="image" />';
    alert(code);
    document.getElementById('test').innerHTML = code;
    alert(document.getElementById('test').innerHTML);
}

Running the code above will show /> in the first alert, but the second alert doesn't, it shows just >. So it looks like applying to .innerHTML strips out the forward slash. Any ideas how to stop this from happening? I need the forward slash for validation.

解决方案

The second alert just shows you what the browser uses as its internal representation of your image element. You don't need the slash for validation, as validation always is about your page's source, validators don't execute JavaScript that dynamically adds elements to the DOM.

In fact, most browsers handle XHTML internally just the same as HTML, not as an XML-representation of your document. Only when you send your XHTML document with MIME-type application/xhtml+xml, some browsers will render your page using the XML parser.

Also see Ian Hickson's article.

这篇关于innerHTML从图像标记中删除关闭斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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