使用innerHTML.replace替换文本来创建链接
我正在使用 Sharepoint (WSS 3.0),不幸的是,它格式化调查问题的能力非常有限(即,它会删除您输入的任何 HTML)。 我在其他地方看到了一个解决方案,建议我们在母版页文件中添加一些 JS 以允许换行。 这工作得很好,但我想看看我们是否也可以允许链接。
在我们的 WSS 调查中,我现在可以在任何需要换行的地方使用 {{br}}(这有效)。 我尝试扩展代码以允许使用链接标签(例如,{{link1}}url{{link2}}URL Title{{link3}};但是,这不起作用,可能是因为更新不作为一个整体发生,然后浏览器尝试将其逐个渲染,使其混淆(FF 和 IE 显示不同的结果,但都失败。如果我混淆了下面 JS 的顺序 - 即,执行 link3, 2。然后 1——输出也发生变化,但仍然失败。)有更好的方法吗?
<script language="JavaScript">
var className;
className = 'ms-formlabel';
var elements = new Array();
var elements = document.getElementsByTagName('td');
for (var e = 0; e < elements.length; e++)
{
if (elements[e].className == className){
elements[e].innerHTML = elements[e].innerHTML.replace(/{{br}}/g,'<br/>');
elements[e].innerHTML = elements[e].innerHTML.replace(/{{link1}}/g,'<a href="');
elements[e].innerHTML = elements[e].innerHTML.replace(/{{link2}}/g,'">');
elements[e].innerHTML = elements[e].innerHTML.replace(/{{link3}}/g,'</a>');}
}
</script>
I'm using Sharepoint (WSS 3.0), which is unfortunately very limited in its ability to format survey questions (i.e., it strips any HTML you enter). I saw a solution elsewhere that suggested we add some JS to our master page file in order to allow line breaks. This works beautifully, but I'd like to see if we can allow links as well.
In our WSS surveys, I can now use {{br}} anywhere I want a line break (this works). I have tried extending the code to allow the use of link tags (e.g., {{link1}}url{{link2}}URL Title{{link3}}; but, this doesn't work, presumably because the updates aren't happening as a whole, and the browser then tries to render it piece by piece, confusing it. (FF and IE show different results, but both fail. If I mix up the order of the JS below -- i.e., do link3, 2 and then 1 -- the output changes as well, but still fails.) Is there a better way to do this?
<script language="JavaScript">
var className;
className = 'ms-formlabel';
var elements = new Array();
var elements = document.getElementsByTagName('td');
for (var e = 0; e < elements.length; e++)
{
if (elements[e].className == className){
elements[e].innerHTML = elements[e].innerHTML.replace(/{{br}}/g,'<br/>');
elements[e].innerHTML = elements[e].innerHTML.replace(/{{link1}}/g,'<a href="');
elements[e].innerHTML = elements[e].innerHTML.replace(/{{link2}}/g,'">');
elements[e].innerHTML = elements[e].innerHTML.replace(/{{link3}}/g,'</a>');}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要分块修改innerHTML属性(每次更改innerHTML时,浏览器都会尝试更新DOM,如果您提供不完整/损坏的标记,显然会弄乱事情),而是针对您自己的字符串变量进行所有修改,然后用完整的字符串覆盖整个innerHTML:
Instead of modifying the innerHTML property in chunks (the browser tries to update the DOM each time you change innerHTML, which if you provide incomplete/broken markup, will obviously mess things up), do all your modifications against your own string variable, and then overwrite the entire innerHTML with your completed string:
简单的答案是构建innerHTML并立即替换它:
更复杂的答案是在正则表达式中使用捕获组并将函数作为第二个参数传递给replace(),以便使用单个调用来替换 HTML 的()。 例如,
第二个解决方案更复杂并导致一些丑陋的正则表达式,但比一遍又一遍地调用replace()更有效。
The simple answer would be to build up the innerHTML and replace it all at once:
The more complex answer would be to use capturing groups in your regex and pass a function as the 2nd parameter to replace(), so as to use a single call to replace() for the HTML. For example,
The second solution is more complex and leads to some ugly regexes, but is more efficient than calling replace() over and over again.