基本的 InnerHTML 问题

发布于 2024-10-21 20:21:31 字数 699 浏览 10 评论 0原文

这是我在循环中使用的一段代码,用于将 HTML 元素附加到 Google Chrome 扩展程序的 popup(.html):

  • wordlist”是一个单词数组。
  • 'rand' 是随机数生成函数。

代码:

for (l=1; l<11; l++) { 
    var i = rand(wordlist.length;
    var h1 = document.createElement("h1");  
    h1.innerHTML = wordlist[i];
    document.body.appendChild(h1);
}

没有预先存在的 HTML 元素。因此,此代码将 10 个随机单词附加到空白页。

现在,我想将链接附加到页面,而不是附加到页面的单词(例如:http://dictionary.reference.com/browse/**wordlist[i]** ,这基本上是该特定单词的 Dictionary.com 查询)。另外,我希望在

PS:我前一天开始学习 HTML 和 JS,我太兴奋了,无法开始编写一些代码。如果我忽略了一个简单的 Google 解决方案来解决我的问题,我深表歉意。感谢您抽出时间。

this is the piece of code that I am using in a loop to append an HTML element to a Google Chrome Extension's popup(.html):

  • 'wordlist' is an array of words.
  • 'rand' is the Random number generating function.

Code:

for (l=1; l<11; l++) { 
    var i = rand(wordlist.length;
    var h1 = document.createElement("h1");  
    h1.innerHTML = wordlist[i];
    document.body.appendChild(h1);
}

There are no pre-existing HTML elements. So, this code is appending 10 random words to a blank page.

Now, instead of the words being appended to the page, I want to append links to the page (eg: http://dictionary.reference.com/browse/**wordlist[i]**, which is basically a dictionary.com query for that particular word). Also, I want this link to be opened in a new Chrome tab when clicked. How do I do this?

P.S: I started learning HTML and JS a day before, and I was too excited to start writing some code. I apologize if I have overlooked a simple Google solution to my problem. Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

撩心不撩汉 2024-10-28 20:21:31
for (l=1; l<11; l++) { 
    var i = rand(wordlist.length);
    var a = document.createElement("a");  
    a.setAttribute('href', 'http://dictionary.reference.com/browse/'.wordlist[i]);
    a.setAttribute('target', '_blank');
    h1.innerHTML = wordlist[i];
    document.body.appendChild(a);
}

基本上只需将 h1 更改为 a 即可。
还要设置属性 href(链接)和 target(应为 openend)。

for (l=1; l<11; l++) { 
    var i = rand(wordlist.length);
    var a = document.createElement("a");  
    a.setAttribute('href', 'http://dictionary.reference.com/browse/'.wordlist[i]);
    a.setAttribute('target', '_blank');
    h1.innerHTML = wordlist[i];
    document.body.appendChild(a);
}

Basicly just change h1 to a.
Also set the attribute href (the link) and target (where it should be openend).

听风吹 2024-10-28 20:21:31

如果您想使用 DOM,

for (l=1; l<11; l++) { 
    var i = rand(wordlist.length;
    var a = document.createElement("a");
    a.href = "http://dictionary.reference.com/browse/" + wordlist[i];
    a.appendChild(document.createTextElement(wordlist[i]));
    document.body.appendChild(h1);
}

下面的代码会更快,因为您只需附加 HTML 一次(并且 DOM 操作的成本很高):

var html = "";
for (l=1; l<11; l++) { 
    var i = rand(wordlist.length);
    html += "<a href='http://dictionary.reference.com/browse/" + wordlist[i] + "'>"
                + wordlist[i]
          + "</a>";
}
document.body.innerHTML += html;

If you want to work with the DOM,

for (l=1; l<11; l++) { 
    var i = rand(wordlist.length;
    var a = document.createElement("a");
    a.href = "http://dictionary.reference.com/browse/" + wordlist[i];
    a.appendChild(document.createTextElement(wordlist[i]));
    document.body.appendChild(h1);
}

The following will be faster, since you'll append the HTML only once (and DOM operations are costly):

var html = "";
for (l=1; l<11; l++) { 
    var i = rand(wordlist.length);
    html += "<a href='http://dictionary.reference.com/browse/" + wordlist[i] + "'>"
                + wordlist[i]
          + "</a>";
}
document.body.innerHTML += html;
指尖微凉心微凉 2024-10-28 20:21:31

尝试将代码更改为以下内容:

for (l=1; l<11; l++) { 
  var i = rand(wordlist.length);
  var a = document.createElement("a");  
  a.innerHTML = wordlist[i];
  a.href = "http://dictionary.reference.com/browse/" + wordlist[i];
  document.body.appendChild(a);
}

我修复了第 2 行中的问题,您省略了右括号。
我还更明显地将你的 h1 更改为 a,并添加了一行设置其 href 的行。

Try changing your code to the following:

for (l=1; l<11; l++) { 
  var i = rand(wordlist.length);
  var a = document.createElement("a");  
  a.innerHTML = wordlist[i];
  a.href = "http://dictionary.reference.com/browse/" + wordlist[i];
  document.body.appendChild(a);
}

I fixed a problem in line 2, you'd omitted a closing parenthesis.
I also more obviously changed your h1 to an a, and added a line that sets its href.

懒的傷心 2024-10-28 20:21:31

嗯,它就像更改

 h1.innerHTML = wordlist[i];

为您想要的一样简单,即:

 h1.innerHTML = "<a href='http://dictionary.reference.com/browse/"+wordlist[i]+"' target='_blank'>"+wordlist[i]+"</a>";

要使其在新选项卡中打开,您必须添加属性“目标”并为其指定空白值。我认为它在 HTML 5 中已被弃用......

Well its as easy as changing

 h1.innerHTML = wordlist[i];

to exatly what you are after, namely:

 h1.innerHTML = "<a href='http://dictionary.reference.com/browse/"+wordlist[i]+"' target='_blank'>"+wordlist[i]+"</a>";

To make it open in a new tab you have to add the attribute "target" and give it a value of blank. I think its deprecated in HTML 5 though...

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