插入的代码产生意外结果
所以最终我想要一个greasemonkey脚本,但现在我只使用firebug来测试我的html/javascript。
我有这段代码可以在博客的 HTML 工具栏中插入一个按钮。 该按钮应该将所有
替换为“”,因为博主似乎只是随机将
添加到我的博客文章中,并可能导致发布的文章看起来很奇怪(用
分隔的一堆单词不会像用“”分隔的相同单词那样在中间中断)。
document.getElementById("postingHtmlToolbar").firstChild.innerHTML += '<div id="SP" class="goog-inline-block goog-toolbar-button" title="SP" role="button" style="-moz-user-select: none;"><div class="goog-inline-block goog-toolbar-button-outer-box"><div class="goog-inline-block goog-toolbar-button-inner-box"><a href="javascript:document.getElementById(\'postingHtmlBox\').value = document.getElementById(\'postingHtmlBox\').value.replace(/ /g, \' \');"><b>SP</b></a></div></div></div>';
去掉格式就只剩下我们了。
<a href="javascript:document.getElementById('postingHtmlBox').value = document.getElementById('postingHtmlBox').value.replace(/ /g, ' ');"><b>SP</b></a>
有趣的是,从 firebug 运行的相同代码(减去 href、javascript: stuff)可以完美运行。
但是当它像这样插入并运行时,它会清空整个网页并写入 document.getElementById('postingHtmlBox').value.replace(/ /g, ' ')
的值> 进入这个黑色页面。
我是不是忘记了什么愚蠢的事情?这应该发生吗?我有一些愚蠢的语法错误吗?您建议采取什么解决方案?
So eventually I want to have a greasemonkey script but for now I have only been working with firebug to test out my html/javascript.
I have this code to insert one button in the HTML toolbar for blogger.
The button is supposed to replace all the
with " " as blogger seems to just randomly add
into my blogs posts and can cause the published article to look weird (a bunch of words separated in between with
s will not want to break in the middle unlike the same words separated with " ").
document.getElementById("postingHtmlToolbar").firstChild.innerHTML += '<div id="SP" class="goog-inline-block goog-toolbar-button" title="SP" role="button" style="-moz-user-select: none;"><div class="goog-inline-block goog-toolbar-button-outer-box"><div class="goog-inline-block goog-toolbar-button-inner-box"><a href="javascript:document.getElementById(\'postingHtmlBox\').value = document.getElementById(\'postingHtmlBox\').value.replace(/ /g, \' \');"><b>SP</b></a></div></div></div>';
Which trimming away the formatting just leaves us with.
<a href="javascript:document.getElementById('postingHtmlBox').value = document.getElementById('postingHtmlBox').value.replace(/ /g, ' ');"><b>SP</b></a>
The funny thing is that that same code (minus the href, javascript: stuff) run from firebug works perfectly.
But when it is inserted like this and run it blanks the entire web page and writes the value of document.getElementById('postingHtmlBox').value.replace(/ /g, ' ')
into this black page.
Am I forgetting something stupid? Is this supposed to happen? Do I have some stupid syntax error? What would you suggest as a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将 JS 代码放入
href
中。使用onclick
:Don't put JS code in
href
. useonclick
:尝试将
void(0);
添加到 href 末尾。基本上,最后一个语句(如果有)的输出正在替换文档(我也遇到了同样的问题),因此使最后一个语句没有输出将避免该问题
Try adding
void(0);
to the end of the href.Basically, the output of the last statement (if any) is replacing the document (i've had the same problem), so making the last statement one with no output will avoid the problem