插入的代码产生意外结果

发布于 2024-12-12 02:11:33 字数 1343 浏览 0 评论 0原文

所以最终我想要一个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(/&nbsp;/g, \' \');"><b>SP</b></a></div></div></div>';

去掉格式就只剩下我们了。

  <a href="javascript:document.getElementById('postingHtmlBox').value = document.getElementById('postingHtmlBox').value.replace(/&nbsp;/g, ' ');"><b>SP</b></a>

有趣的是,从 firebug 运行的相同代码(减去 href、javascript: stuff)可以完美运行。

但是当它像这样插入并运行时,它会清空整个网页并写入 document.getElementById('postingHtmlBox').value.replace(/&nbsp;/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-12-19 02:11:33

不要将 JS 代码放入 href 中。使用onclick

<a href="javascript//" onclick="document.getElementById('postingHtmlBox').value = document.getElementById('postingHtmlBox').value.replace(/ /g, ' ');"><b>SP</b></a>

Don't put JS code in href. use onclick:

<a href="javascript//" onclick="document.getElementById('postingHtmlBox').value = document.getElementById('postingHtmlBox').value.replace(/ /g, ' ');"><b>SP</b></a>
迷途知返 2024-12-19 02:11:33

尝试将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文