![](/img/trans.png)
[英]PHP: How to add a CSS text-align property to an already present inline style attribute of an element using simple_html_dom parser?
[英]how to change text using style property in html, css
在此示例中,我有一个必须更改文本的按钮。 现在,我知道可以使用onclick=document.getElementById('id').innerHTML="text"
但是我正在制作一个函数,使
**onclick = myFunc()**
<script>
function myFunc()
var x = document.getElementById('id');
x.style.property = 'text';
</script>
所以我的问题是,是否可以使用任何可以更改文本的属性
不可以。“样式”属性用于样式设置。 除了一些非常具体的例外,它们仅用于更改内容的外观。 不更改内容本身。
innerHTML还有其他替代方法。 innerHTML用于将HTML内容添加到元素。
一种替代方法是textContent
。 将纯文本添加到元素。
var x = document.getElementById('foo'); x.textContent = 'text';
<button id="foo">Hello World</button>
您可以使用设置文本:
<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.