Javascript javascript中的html标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7096820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:50:18  来源:igfitidea点击:

Html tag inside javascript

javascript

提问by user899043

I am trying to use some html tag inside Javascript.But, Html Tag does not work. How can i use html tag inside javascript. I wanted to use h1 tag inside Javascript but did not work:(

我正在尝试在 Javascript 中使用一些 html 标签。但是,Html 标签不起作用。我如何在 javascript 中使用 html 标签。我想在 Javascript 中使用 h1 标签,但没有用:(

     <script type="text/javascript">
    if(document.getElementById('number1').checked) {

             <h1>Hello member</h1>}
       </script>

回答by Richard JP Le Guen

You will either have to document.writeit or use the Document Object Model:

您将不得不document.write使用它或使用文档对象模型:

Example using document.write

使用示例 document.write

<script type="text/javascript">
if(document.getElementById('number1').checked) {
    document.write("<h1>Hello member</h1>");
}
</script>

Example using DOM

使用DOM 的示例

<h1></h1> <!-- We are targeting this tag with JS. See code below -->
<input type="checkbox" id="number1" checked /><label for="number1">Agree</label>
<div id="container"> <p>Content</p> </div>

<script type="text/javascript">
window.onload = function() {
    if( document.getElementById('number1').checked ) {
        var h1 = document.createElement("h1");
        h1.appendChild(document.createTextNode("Hello member"));
        document.getElementById("container").appendChild(h1);
    }
}
</script>

回答by PaulaD

This is what I used for my countdown clock:

这是我用于倒计时的时钟:

</SCRIPT>
<center class="auto-style19" style="height: 31px">
<Font face="blacksmith" size="large"><strong>
<SCRIPT LANGUAGE="JavaScript">
var header = "You have <I><font color=red>" 
+ getDaysUntilICD10() + "</font></I>&nbsp; "
header += "days until ICD-10 starts!"
document.write(header)
</SCRIPT>

The HTML inside of my script worked, though I could not explain why.

我的脚本中的 HTML 有效,但我无法解释原因。

回答by Bahman.A

here's how to incorporate variables and html tags in document.write also note how you can simply add text between the quotes

这是在 document.write 中合并变量和 html 标签的方法,还请注意如何简单地在引号之间添加文本

document.write("<h1>System Paltform: ", navigator.platform, "</h1>");

回答by Kwilver Bear

If you write HTML into javascript anywhere, it will think the HTML is written in javascript. The best way to include HTML in JavaScript is to write the HTML code on the page. The browser can't display HTML tags, so the browser will recognize the HTML and write this code in HTML.

如果您在任何地方将 HTML 写入 javascript,它都会认为 HTML 是用 javascript 编写的。在 JavaScript 中包含 HTML 的最佳方法是在页面上编写 HTML 代码。浏览器无法显示 HTML 标签,因此浏览器会识别 HTML 并将此代码写入 HTML。

document.write("<p>Hello World!</p>");

Though if you would like to write HTML on a function, GetElementByIdis the best:

但是,如果您想在函数上编写 HTML,这GetElementById是最好的:

function functionName() {
   document.getElementById(" the id of the HTML element to be written in ").innerHTML = "whatever you want to say"
}

Hope this helps!

希望这可以帮助!

回答by praba230890

JavaScript is a scripting language, not a HTMLanguage type. It is mainly to do process at background and it needs document.writeto display things on browser. Also if your document.writeexceeds one line, make sure to put concatenation +at the end of each line.

JavaScript 是一种脚本语言,而不是 HTML 语言类型。它主要是在后台进行处理,需要document.write在浏览器上显示内容。此外,如果您document.write超过一行,请确保+在每一行的末尾连接。

Example

例子

<script type="text/javascript">
if(document.getElementById('number1').checked) {
document.write("<h1>Hello" +
"member</h1>");
}
</script>

回答by Bishrul Haq

<div id="demo"></div>

<input type="submit" name="submit" id="submit" value="Submit" onClick="return empty()">


<script type="text/javascript">
        function empty()
        {
          var x;
          x = document.getElementById("feedbackpost").value;
          if (x == "")
           {
             var demo = document.getElementById("demo");
             demo.innerHTML =document.write='<h1>Hello member</h1>';
              return false;
           };
        }
    </script>

回答by user7644865

<html>
 <body>
  <input type="checkbox" id="number1" onclick="number1();">Number 1</br>
  <p id="h1"></p>
   <script type="text/javascript">
    function number1() {
    if(document.getElementById('number1').checked) {

      document.getElementById("h1").innerHTML = "<h1>Hello member</h1>";
         }
    }
   </script>
 </body>
</html>

回答by GBa

   <div id="demo></div>

   <script type="text/javascript">
        if(document.getElementById('number1').checked) {
             var demo = document.getElementById("demo");    
             demo.innerHtml='<h1>Hello member</h1>';
        } else {
             demo.innerHtml='';
        }
    </script>