Ich versuche, den Inhalt von var fruits
(Array) in einer HTML-Seite mit li, ul, a und href für ein bestimmtes Wort (nicht den gesamten Text) zu zeigen .href in einem bestimmten Wort der Zeichenfolge mit JavaScript und HTML5
Zum Beispiel für den Satz I want a link here
Ich mag den Link nur für das Wort here
aber ich weiß nicht, wie es zu tun ...
Hier ist der Code so weit:
<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>
Es funktioniert! Ich danke dir sehr – Gera