简体   繁体   English

如何使用HTML,CSS中的style属性更改文本

[英]how to change text using style property in html, css

in this example, i have a button which has to change the text. 在此示例中,我有一个必须更改文本的按钮。 Now I know that I can use the onclick=document.getElementById('id').innerHTML="text" but I am making a function such that 现在,我知道可以使用onclick=document.getElementById('id').innerHTML="text"但是我正在制作一个函数,使

**onclick = myFunc()**


 <script>
    function myFunc()
    var x = document.getElementById('id');
    x.style.property = 'text';
    </script>

so my question is , is there any property I can use which will change the text 所以我的问题是,是否可以使用任何可以更改文本的属性

No. "Style" properties are for styling. 不可以。“样式”属性用于样式设置。 With a few very specific exceptions, they are only for changing the look of the content. 除了一些非常具体的例外,它们仅用于更改内容的外观。 Not changing the content itself. 不更改内容本身。

There are other alternatives to innerHTML. innerHTML还有其他替代方法。 innerHTML is for adding HTML content to the element. innerHTML用于将HTML内容添加到元素。

One alternative is textContent . 一种替代方法是textContent Which adds plain text to the element. 将纯文本添加到元素。

 var x = document.getElementById('foo'); x.textContent = 'text'; 
 <button id="foo">Hello World</button> 

You can set text using : 您可以使用设置文本:

<script>
    function myFunc(){
      var x = document.getElementById('btntext');
      x.style.color = "green";
      x.style.fontSize = "large";
      x.value = "This is new TExt";
   }
</script>
<body>
<input type="text" id="btntext" value="This test"/>
<button onClick="myFunc()" name="send">change color</button>
</body>

 function changeText(id) { id.innerHTML = "Text has been changed!"; } 
 <h1 onclick="changeText(this)">Click on this text!</h1> 

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

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