2017-06-13 40 views
-6

如何使用Javascript替換HTML文本?更改文本的JavaScript

在片段下面,我需要替換文本「作者說」與「作者簡介」。

謝謝!

<div id="author-info"> 
    <div class="author-image"> 
     <a class="author-avatar" href="https://www.test.com"><img alt=""></a> 
    </div> 
    <div class="author_name_wrap"> 
     <h4 class="author_name"><a href="https://www.test.com/author/">Author say</a></h4> 
     <div class="author_socicons"></div>  </div> 
</div> 
+0

? – Chris

+0

你想怎麼更換呢?你能有點特別嗎? – dloeda

+0

我想要在整個網站上發生這種情況 – Ira

回答

-1
$("h4.author_name a").text('About the Author'); 

你可以找到詳細的文章here

+0

請注意,jQuery的既不是標記,也沒有在問題中提到。我個人認爲,如果OP沒有提及或標記它們,應該避免框架答案。無論如何,我認爲發佈一行jQuery代碼,而沒有提到你使用框架/外部資源是非常沒用的,特別是當OP不熟悉JS做文本替換時。 – Santi

+0

謝謝!看起來這個鏈接不起作用。 – Ira

0

要更改HTML標記使用的innerHTML屬性的內容:你想要什麼樣的事件這種變化發生

var h4 = document.getElementsByClassName("author_name")[0], 
    target = h4.childNodes[0]; 
target.innerHTML = "About the author";