简体   繁体   English

用纯JavaScript将html字符串替换为img标签

[英]Replace html string to img tag with pure javascript

I want javascript to do this: 我希望javascript执行此操作:

According the word that is in the div, i want the string be replaced with a img tag. 根据div中的单词,我希望将字符串替换为img标签。

if i have 如果我有

<div id="example" style="align-content: right; text-align: right">abc</div> 

with ` 与`

<div id="example" style="align-content: right; text-align: right"><img src="example123.html"></div>`

But only if inside the div is the text/string "abc" 但仅当div内是文本/字符串“ abc”时

Another case, if i have 另一种情况,如果我有

<div id="example" style="align-content: right; text-align: right">def</div>

i want to convert into this 我想转换成这个

<div id="example" style="align-content: right; text-align: right"><img src="anotherexample.html"></div>

As the similar of the 1st case, replace with another img in case of the text be "def". 与第一种情况类似,如果文本为“ def”,则替换为另一个img。

I have done this with jquery, 我已经用jQuery完成了

This was my code done in jquery 这是我在jquery中完成的代码

$("#example:contains('abc')").html(function (_, html) {return html.replace(/abc/g, "<img src=example123.html>")});

$("#example:contains('def')").html(function (_, html) {return html.replace(/def/g, "<img src=anotherexample.html>")});

It was easy to do, but i have some problems using jquery in my work, so i can't and i shouldn't use jquery or another framework. 这很容易做到,但是我在工作中使用jquery时遇到了一些问题,因此我不能也不应使用jquery或其他框架。 It would be great if it was done in pure javascript. 如果它是用纯JavaScript完成的,那就太好了。

I almost forgot that it would be great using conditionals, specially swith, i think using switch instead of if else could be less tangled 我几乎忘记了使用条件句(特别是Swith)会很棒,我认为使用switch而不是其他方式可以减少纠结

Thanks in advance for your answers. 预先感谢您的回答。

PD: if you ask me why i need to solve this, the reason is that i am using an application that generates pdf files extracting the data from a DB, and according to the text, replace with the pics with a specific URL. PD:如果您问我为什么需要解决这个问题,原因是我正在使用一个生成pdf文件的应用程序,该文件会从DB中提取数据,并根据文本,用具有特定URL的图片替换。

Because you're using an element with a particular id , it's unique - all you need to do is select that element and check to see if its innerHTML includes the string you're looking for. 因为您使用的是具有特定id的元素,所以它是唯一的-您只需选择该元素并检查其innerHTML包含您要查找的字符串即可。 You could use a regular expression to replace all instances of abc or def with the appropriate tag: 您可以使用正则表达式将abcdef所有实例替换为适当的标记:

 const example = document.querySelector('#example'); const srcs = { abc: 'example123', def: 'anotherexample' }; example.innerHTML = example.innerHTML.replace( /abc|def/g, prop => `<img src="${srcs[prop]}">` ); 
 <div id="example" style="align-content: right; text-align: right">before abc after</div> 

Try this. 尝试这个。 It iterates over all the div elements and checks their text using textContent .If it matches it replaces the text with image tag using innerHTML 它遍历所有div元素并使用textContent检查其text 。如果匹配,则使用innerHTML用image标签替换文本。

 var a = document.querySelectorAll('div'); a.forEach((e) => { if (e.textContent == "abc") e.innerHTML = "<img src='example123.com'>" if(e.textContent == "def") e.innerHTML = "<img src='anotherexample.com'>" }) 
 <div id="example" style="align-content: right; text-align: right">abc</div> <div id="example" style="align-content: right; text-align: right">def</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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