第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用javascript用超鏈接替換單詞?

如何使用javascript用超鏈接替換單詞?

冉冉說 2024-01-18 14:42:43
我想用我網(wǎng)站上每個帖子上的超鏈接替換一個單詞[僅一次],在我的例子中是“羅納爾多”。所以,我使用了以下代碼。document.body.innerHTML = document.body.innerHTML.replace('Ronaldo', '<a href="www.ronaldo.com">Ronaldo</a>');在我注意到這個問題之前,這確實很有效。post-title當(dāng)我希望它只替換“in ”這個詞時,它甚至替換了“Ronaldo”這個詞post-body。這是我的代碼的一瞥,以便您可以更好地理解。https://codepen.io/vkdatta27/pen/rNMGbmj [更新]如果有人提出解決此問題的方法,那將非常有幫助。我標(biāo)記 jquery 和 ajax 因為它們也了解 javascript。注意:到目前為止,除了格式化目的POST-BODY P之外,我們沒有使用任何類、IDS、標(biāo)簽等POST-TITLE
查看完整描述

5 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗 獲得超4個贊

嘗試這個 -


postBodyElems = document.getElementsByTagName("p");

for (var i = 0; i < postBodyElems.length; i++) {

  if (postBodyElems[i].className == '') {

    postBodyElems[i].innerHTML = postBodyElems[i].innerHTML.replace('Ronaldo', '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

  }

}

.post-title {

  font-size: 20px;

}


.warning {

  color: red;

}


a {

  color: blue;

}

<p class='post-title'>Ronaldo became the hottest player of 2020</p>

<p>Ronaldo [<span class='warning'>I want this text to turn into a hyperlink</span>] dos Santos Aveiro GOIH ComM (Portuguese pronunciation: [k?i??tj?nu ???na?du]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for Serie

  A club Juventus and captains the Portugal national team. Often considered the best player in the world and widely regarded as one of the greatest players of all time,[10] Ronaldo has won five Ballon d'Or awards[note 3] and four European Golden Shoes,

  both of which are records for a European player. He has won 30 major trophies in his career, including seven league titles, five UEFA Champions Leagues, one UEFA European Championship, and one UEFA Nations League title. Ronaldo holds the records for

  the most goals (134) and assists (41) in the history of the UEFA Champions League.[11] He is one of the few recorded players to have made over 1,000 professional career appearances and has scored over 750 senior career goals for club and country.[12]

  He is also the second player to score 100 international goals, and the first European to achieve the feat.[13]</p>


查看完整回答
反對 回復(fù) 2024-01-18
?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗 獲得超10個贊

假設(shè)您的post-body元素沒有任何類名,我們可以通過使用查詢它們.getElementsByTagName(),然后用鏈接替換文本


postBodyElems = document.getElementsByTagName("p");

for (var i = 0; i < postBodyElems.length; i++) {

  if (postBodyElems[i].className == '') {

    postBodyElems[i].innerHTML = postBodyElems[i].innerHTML.replace('Ronaldo', '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

  }

}

.post-title {

  font-size: 20px;

}


.warning {

  color: red;

}


a {

  color: blue;

}

<p class='post-title'>Ronaldo became the hottest player of 2020</p>

<p>Ronaldo [<span class='warning'>I want this text to turn into a hyperlink</span>] dos Santos Aveiro GOIH ComM (Portuguese pronunciation: [k?i??tj?nu ???na?du]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for Serie

  A club Juventus and captains the Portugal national team. Often considered the best player in the world and widely regarded as one of the greatest players of all time,[10] Ronaldo has won five Ballon d'Or awards[note 3] and four European Golden Shoes,

  both of which are records for a European player. He has won 30 major trophies in his career, including seven league titles, five UEFA Champions Leagues, one UEFA European Championship, and one UEFA Nations League title. Ronaldo holds the records for

  the most goals (134) and assists (41) in the history of the UEFA Champions League.[11] He is one of the few recorded players to have made over 1,000 professional career appearances and has scored over 750 senior career goals for club and country.[12]

  He is also the second player to score 100 international goals, and the first European to achieve the feat.[13]</p>


查看完整回答
反對 回復(fù) 2024-01-18
?
拉丁的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超8個贊

如果您不想更改整個文檔,請不要從 DOM 中獲取整個內(nèi)容。你說你只是想改變后身。因此,給帖子正文一個id(這樣我們就可以在js中獲取帖子正文)并僅更改其內(nèi)容。


注意:要替換所有出現(xiàn)的“Ronaldo”,請使用replaceAll("word to replace")函數(shù)。


document.getElementById("post-body").innerHTML = document.getElementById("post-body").innerHTML.replaceAll('Ronaldo', '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

.post-title {

  font-size: 20px;

}


.warning {

  color: red;

}


a {

  color: blue;

}

<p class='post-title'>Ronaldo became the hottest player of 2020</p>


<!--Give the following p element an id, so we can get that element in js. I gave the 'post-body' id. -->

<p id='post-body'>Ronaldo [<span class='warning'>I want this text to turn into a hyperlink</span>] dos Santos Aveiro GOIH ComM (Portuguese pronunciation: [k?i??tj?nu ???na?du]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for Serie

  A club Juventus and captains the Portugal national team. Often considered the best player in the world and widely regarded as one of the greatest players of all time,[10] Ronaldo has won five Ballon d'Or awards[note 3] and four European Golden Shoes,

  both of which are records for a European player. He has won 30 major trophies in his career, including seven league titles, five UEFA Champions Leagues, one UEFA European Championship, and one UEFA Nations League title. Ronaldo holds the records for

  the most goals (134) and assists (41) in the history of the UEFA Champions League.[11] He is one of the few recorded players to have made over 1,000 professional career appearances and has scored over 750 senior career goals for club and country.[12]

  He is also the second player to score 100 international goals, and the first European to achieve the feat.[13]</p>


只是上面代碼片段的 js 的干凈版本。


const postBody = document.getElementById("post-body");

const ronaldoLink = '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>'


postBody.innerHTML = postBody.innerHTML.replaceAll('Ronaldo', ronaldoLink);


查看完整回答
反對 回復(fù) 2024-01-18
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗 獲得超7個贊

只是改變

文檔主體

document.getElementById("ID") //相應(yīng)的元素id即你的段落

document.getElementById("post-body").innerHTML = document.getElementById("post-body").innerHTML.replaceAll('Ronaldo', '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

.post-title{

  font-size:20px;

  }

.warning{

 color:red;

  }

a{

  color:blue;

  }

<p class='post-title'>Ronaldo became the hottest player of 2020</p>

<p id='post-body'>Ronaldo [<span class='warning'>I want this text to turn into a hyperlink</span>] dos Santos Aveiro GOIH ComM (Portuguese pronunciation: [k?i??tj?nu ???na?du]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for Serie A club Juventus and captains the Portugal national team. Often considered the best player in the world and widely regarded as one of the greatest players of all time,[10] Ronaldo has won five Ballon d'Or awards[note 3] and four European Golden Shoes, both of which are records for a European player. He has won 30 major trophies in his career, including seven league titles, five UEFA Champions Leagues, one UEFA European Championship, and one UEFA Nations League title. Ronaldo holds the records for the most goals (134) and assists (41) in the history of the UEFA Champions League.[11] He is one of the few recorded players to have made over 1,000 professional career appearances and has scored over 750 senior career goals for club and country.[12] He is also the second player to score 100 international goals, and the first European to achieve the feat.[13]</p>



查看完整回答
反對 回復(fù) 2024-01-18
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗 獲得超7個贊

你需要使用replaceAll而不是replace

document.body.innerHTML = document.body.innerHTML.replaceAll('Ronaldo', '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

或者您可以使用regex(這也可以用作不區(qū)分大小寫的替換)-

document.body.innerHTML = document.body.innerHTML.replace(/Ronaldo/gi, '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

如果您不想更改課堂上的文本,post-title您可以使用not查詢選擇器 -

document.querySelector("p:not(.post-title)").innerHTML = 
document.querySelector("p:not(.post-title)").innerHTML.replaceAll('Ronaldo', '<a href="https://en.wikipedia.org/wiki/Cristiano_Ronaldo">Cristiano Ronaldo</a>');

這只會選擇第一個p沒有 class 的 post-title

如果您有多個p標(biāo)簽,請使用querySelectorAll并迭代它來替換文本。

或者,您可以向內(nèi)容添加不同的類,并在查詢選擇器中使用此類。


查看完整回答
反對 回復(fù) 2024-01-18
  • 5 回答
  • 0 關(guān)注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號