简体   繁体   中英

How to replace a text with hyperlink using javascript?

I want to replace a word with hyperlink on every post. So i used this code.

document.body.innerHTML = document.body.innerHTML.replace('Ronaldo', '<a href="www.ronaldo.com">Ronaldo</a>');

This code is working properly but the issue is, it's also replacing Ronaldo in title and in heading. I don't want that. I want that code to only replace words in post-body and not on post-title or <h> tags

I'm tagging jquery and ajax because they too know javascript.

You are selecting whole body with: document.body; when you can just select the specific one by using: document.getElementById("yourId"); and replaces it's Ronaldo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM