简体   繁体   English

使用insertAdjacentHTML

[英]Using insertAdjacentHTML with <a href…>

I am trying to modify a webpage with a chrome plugin I wrote myself. 我正在尝试使用我自己编写的chrome插件修改网页。 I'd like to add an additional link before a existing link. 我想在现有链接之前添加其他链接。

I am using insertAdjacentHTML to add my new link before the original link. 我使用insertAdjacentHTML在原始链接之前添加我的新链接。 But as soon as I use the a href it fails. 但是一旦我使用了一个href就失败了。

UPDATE There were syntax errors in my code as many pointed out. 更新许多人指出我的代码中存在语法错误。 I fixed these 我修好了这些

This is the new code (but still won't work...webpage I want to alter seems to load forever and nothing happens): 这是新的代码(但仍然无法正常工作...我想改变的网页似乎永远加载,没有任何反应):

var anchors = document.getElementsByTagName("a");

for (var i = 0; i < anchors.length; i++) {
   anchors[i].href =   "javascript:void(0)";
   anchors[i].insertAdjacentHTML("beforebegin", "<a href='www.google.de'> bold text</a>")
} 

Somehow the whole code works, if I don't add "a href". 如果我不添加“a href”,整个代码就可以了。 So this works: 这样可行:

anchors[i].insertAdjacentHTML("beforebegin", "<b> bold text</b>")

If I try the tag ..it somehow fails - can you give me a hint why? 如果我尝试标签..某种程度上失败了 - 你能给我一个提示吗?

Update2 I was finally able to solve this: I accidentally create an infinite loop. Update2我终于能够解决这个问题了:我不小心创建了一个无限循环。 @Andy's answer points out a solutions to this. @Andy的回答指出了解决方案。

Aside from the typos the big problem with the code is that getElementsByName returns a live list of anchor nodes . 除了拼写错误之外,代码的一个大问题是getElementsByName返回一个锚节点实时列表 This can be useful in the right circumstances, but with your loop you're adding a new anchor to the list with each iteration so the loop is never able to finish and the browser hangs. 这在适当的情况下非常有用,但是在循环中,每次迭代都会向列表中添加一个新锚点 ,因此循环永远无法完成,浏览器也会挂起。 To prevent this use querySelectorAll . 为了防止这种情况,请使用querySelectorAll It returns a static list instead. 它返回一个静态列表。

 var anchors = document.querySelectorAll('a'); for (var i = 0; i < anchors.length; i++) { const newAnchor = '<a href="www.google.de">bold text</a>'; anchors[i] = 'javascript:void(0)'; anchors[i].insertAdjacentHTML('beforebegin', newAnchor); } 
 <a href="www.google.com">Google</a> <a href="www.bert.com">Bert</a> <a href="www.ernie.com">Ernie</a> 

In the long-run if you're going to be coding with JS and HTML a lot you'll probably find it easier to always use single quotes for JS strings etc, and double quotes for HTML attributes. 从长远来看,如果你要用JS和HTML进行编码很多,你可能会发现总是使用单引号用于JS字符串等,以及双引号用于HTML属性更容易。 That way the number of times you'll have to escape quotes will be limited. 这样你将不得不逃避报价的次数将是有限的。

You'll also find using a decent code editor like VSCode will help you see the typos in your code before they get to the browser. 您还会发现使用像VSCode这样不错的代码编辑器可以帮助您在访问浏览器之前查看代码中的拼写错误。

You have a typo where you're setting the href: 你有一个拼写错误,你正在设置href:

anchors[i].href =   "javascript:void(0)

You've opened a string there and not closed it. 你在那里打开了一个字符串而没有关闭它。 Simply add a " on the end. 只需添加一个"最后。

Using a text editor with some decent syntax highlighting can help avoid these sorts of issues (although JS syntax highlighting is often a bit weird for this specific problem). 使用具有一些不错的语法突出显示的文本编辑器可以帮助避免这些类型的问题(尽管JS语法突出显示对于这个特定问题通常有点奇怪)。

You would also have seen an error if you viewed the console in your browser developer tools (for most browsers these are opened by pressing F12 ). 如果您在浏览器开发人员工具中查看控制台,您也会看到错误(对于大多数浏览器,按F12打开它们)。

Check you code, missing " and ; . 检查你的代码,缺少";

var anchors = document.getElementsByTagName("a");

for (var i = 0; i < anchors.length; i++) {
  anchors[i].href =   "javascript:void(0)";
  anchors[i].insertAdjacentHTML("beforebegin", "<a href='www.google.de'> bold text</a>")
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有 React 组件的 insertAdjacentHTML - using insertAdjacentHTML with a React Component 使用insertAdjacentHTML()添加部分HTML - Using insertAdjacentHTML() to add partial HTML JS:用insertAdjacentHTML移动锚点只会附加href,而不是元素 - JS: Moving an anchor with insertAdjacentHTML only appends the href, not the element 如何使用 insertAdjacentHTML select 插入元素? - How to select an element insert using insertAdjacentHTML? 使用innerHTML / insertAdjacentHTML和JS替换Div内容? - Replacing Div Content Using innerHTML/insertAdjacentHTML and JS? 是否可以使用insertAdjacentHTML()插入标签以包装某个元素? - Is it possible to insert tags using insertAdjacentHTML() in order to wrap a certain element? 成功删除内容后,无法使用 insertAdjacentHTML 添加内容 - Unable to add content using insertAdjacentHTML after i have successfully removed it 如何在Javascript中使用.insertAdjacentHTML使用“跨度”设计文本? - How can I design text with “span” using .insertAdjacentHTML in Javascript? 如何通过使用insertAdjacentHTML方法在文章内显示javascript广告单元 - How to display javascript ad unit inside an article by using insertAdjacentHTML method 使用 insertAdjacentHTML 插入元素时,queryselector 不再拾取该元素 - When inserting an element using insertAdjacentHTML, the element is no longer picked up by queryselector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM