在 IE 中使用 javascript 检测制表符
我想检测并替换一些 HTML 中的制表符,如下所示:
<code>
something
{ something else
}
</code
现在,我正在使用类似这样的内容:
$(this).html(replaceAll(trim($(this).html()), "\t", " "));
但是 IE 非常聪明,将制表符更改为空格,因此执行上述操作是无用的。有谁知道如何使用 IE 的 javascript 检测 HTML 源中的制表符?
I want to detect and replace tab characters in a bit of HTML like this:
<code>
something
{ something else
}
</code
Right now, I am using something like this:
$(this).html(replaceAll(trim($(this).html()), "\t", " "));
But IE, in all its cleverness, changes tab characters into spaces, and so doing the above is useless. Does anyone know how I can detect tab characters in the HTML source with javascript for IE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,jmaglasang给了我一个好主意。他说 IE 尊重前置标签中的空白。所以,我想为什么不用 javascript 插入一个 pre 标签,读取 html,然后删除 pre 标签。它可以工作,但有一个问题 - 你必须使用 setTimeout 回调。代码如下:
setTimeout 是必要的,因为由于某种原因,IE 会等待所有 javascript 完成运行后再重新渲染 html。顺便说一句,它还等待执行 setTimeout 发出的任何回调。我希望我知道如何强制 IE 立即渲染 html...如果有人知道我一定会很感激。
So, jmaglasang gave me a good idea. He said IE respects whitespace in a pre tag. So, I thought why not insert a pre tag with javascript, read the html, then remove the pre tag afterward. It works but theres a catch - you have to use a setTimeout callback. Heres the code:
The setTimeout is neccessary because for some reason, IE waits to re-render the html until after all the javascript finishes running. Incidentally, it also waits to execute any callbacks issued by setTimeout. I wish I knew how I could force IE to render the html immediately... If anyone knows I'd definitely appreciate it.