特定の単語(すべてのテキストではない)に対してli、ul、a、およびhrefを使用してHTMLページにvar fruits
(配列)のコンテンツを表示しようとしています。 。JavaScriptとHTML5を使用している特定の単語のhref
例えば、文I want a link here
のために、私は唯一の単語here
のリンクをしたいが、私はどのように行うのか分からない...ここで
コードはこれまでです:
<script>
// selects the div with an id of placeholder
var div = document.getElementById('placeholder');
// say that fruits contains all your data
var fruits = ['I want a link here','I want a link here','I want a link here','I want a link here','I want a link here'],
ul = document.createElement('ul'); // create an arbitrary ul element
// loop through the fruits array
for(var i in fruits) {
// create an arbitrary li element
var li = document.createElement('li'),
content = document.createTextNode(fruits[i]); // create a textnode to the document
var link = "http://google.com";
var element = document.createElement("a");
element.setAttribute("href", link);
element.innerHTML = fruits[i];
li.appendChild(element); // append the created textnode above to the li element
ul.appendChild(li); // append the created li element above to the ul element
}
div.appendChild(ul); // finally the ul element to the div with an id of placeholder
</script>
どうもありがとうございます – Gera