简体   繁体   English

在JavaScript中添加html标签

[英]Adding html tags inside JavaScript

I am trying to add <button id="mybtn"></button> around the some text link but I am not sure how to make it. 我正在尝试在some text链接周围添加<button id="mybtn"></button> ,但是我不确定如何做到这一点。 I tried a bit with document.write but I think I am using it wrong. 我用document.write尝试了一下,但我认为使用错误。 When I click this button some text it replaces 'ZZZ' with nothing. 当我单击此按钮时, some text将全部替换为“ ZZZ”。 However the changed link something here has a plain formatting. 但是, something here更改的链接具有纯格式。 What I want it to do is add the <button id="mybtn"> and </button> around it, so the changed button stays with the same look. 我要执行的操作是在其周围添加<button id="mybtn"></button> ,以便更改后的按钮保持相同的外观。

Input: 输入:

document.getElementById("DiacriticsButton").innerHTML = "some text";

Output should be something like: 输出应该是这样的:

document.getElementById("DiacriticsButton").innerHTML = <button id="mybtn">"some text"</button>;

The current full script: 当前的完整脚本:

<script>
bodyText=document.getElementById("bodytext").innerHTML;
clearText = bodyText.replace(/ZZZ/g, "");
FStr = clearText.search("some text");

do {
  clearText = clearText.replace("something here","some text");  
  FStr = clearText.search("something here");
}
while(FStr!=-1);

function switchDiacritics() {
  if (document.getElementById("bodytext").innerHTML == clearText) {
    document.getElementById("bodytext").innerHTML = bodyText;
    document.getElementById("DiacriticsButton").innerHTML = "something here";
  } else {
    document.getElementById("bodytext").innerHTML = clearText;
    document.getElementById("DiacriticsButton").innerHTML = "some text";
  }
}

</script>

Are you doing anything to trigger this innerHTML change? 您是否正在采取任何措施来触发innerHTML更改? Ex: 例如:

Wrapping the selection in window.onLoad{...} 将选择内容包装在window.onLoad{...}

OR 要么

Wrapping it in a function to be triggered by an event listener. 将其包装在要由事件侦听器触发的函数中。 While if your goal is to simply place text within the tag in a static demeanour use the HTML attribute for a button with value="some text" 如果您的目标是将文本简单地以静态方式放置在标签中,则可以将HTML属性用于具有value="some text"的按钮

Do you look for something like this 你在找这样的东西吗

document.getElementById("DiacriticsButton").innerHTML = '<button id="mybtn">"some text"</button>';

It is also possible to add element like this 也可以添加这样的元素

var btn = document.createElement("button");
btn.innerHTML = "some text";
document.getElementById("DiacriticsButton").appendChild(btn);

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

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