use*_*851 234 javascript dom
是什么区别innerHTML
,innerText
并且childNodes[].value
在JavaScript?
fny*_*fny 250
以下示例引用以下HTML代码段:
<div id="test">
Warning: This element contains <code>code</code> and <strong>strong language</strong>.
</div>
Run Code Online (Sandbox Code Playgroud)
该节点将由以下JavaScript引用:
var x = document.getElementById('test');
Run Code Online (Sandbox Code Playgroud)
element.innerHTML
x.innerHTML
// => "
// => Warning: This element contains <code>code</code> and <strong>strong language</strong>.
// => "
Run Code Online (Sandbox Code Playgroud)
这是W3C的DOM解析和序列化规范的一部分.请注意,它是Element
对象的属性.
node.innerText
x.innerText
// => "Warning: This element contains code and strong language."
Run Code Online (Sandbox Code Playgroud)
innerText
是由微软推出的,有一段时间没有得到Firefox的支持.2016年8月,innerText
被WHATWG采用并在v45中添加到Firefox中.innerText
为您提供样式感知的文本表示,尝试匹配浏览器呈现的内容,这意味着:
innerText
适用text-transform
和white-space
规则innerText
修剪线条之间的空白区域,并在项目之间添加换行符innerText
不会返回不可见项目的文本innerText
将返回textContent
从未呈现的元素<style />
和`Node
元素的属性
node.textContent
x.textContent
// => "
// => Warning: This element contains code and strong language.
// => "
Run Code Online (Sandbox Code Playgroud)
虽然这是W3C标准,但IE <9不支持.
Node
元素的属性
node.value
这个取决于您所针对的元素.对于上面的示例,x
返回HTMLDivElement对象,该对象没有value
定义属性.
x.value // => null
Run Code Online (Sandbox Code Playgroud)
<input />
例如,输入标签()确定一个value
属性,该属性引用"控件中的当前值".
<input id="example-input" type="text" value="default" />
<script>
document.getElementById('example-input').value //=> "default"
// User changes input to "something"
document.getElementById('example-input').value //=> "something"
</script>
Run Code Online (Sandbox Code Playgroud)
来自文档:
注意:对于某些输入类型,返回的值可能与用户输入的值不匹配.例如,如果用户在a中输入非数字值
<input type="number">
,则返回的值可能是空字符串.
这是一个显示上面显示的HTML输出的示例:
var properties = ['innerHTML', 'innerText', 'textContent', 'value'];
// Writes to textarea#output and console
function log(obj) {
console.log(obj);
var currValue = document.getElementById('output').value;
document.getElementById('output').value = (currValue ? currValue + '\n' : '') + obj;
}
// Logs property as [propName]value[/propertyName]
function logProperty(obj, property) {
var value = obj[property];
log('[' + property + ']' + value + '[/' + property + ']');
}
// Main
log('=============== ' + properties.join(' ') + ' ===============');
for (var i = 0; i < properties.length; i++) {
logProperty(document.getElementById('test'), properties[i]);
}
Run Code Online (Sandbox Code Playgroud)
<div id="test">
Warning: This element contains <code>code</code> and <strong>strong language</strong>.
</div>
<textarea id="output" rows="12" cols="80" style="font-family: monospace;"></textarea>
Run Code Online (Sandbox Code Playgroud)
小智 130
innerText
但是,与innerHTML
您不同的是,您可以使用HTML富文本,而不会自动对文本进行编码和解码.换句话说,innerText
检索标记的内容并将其设置为纯文本,而innerHTML
以HTML格式检索和设置内容.
guy*_*mid 22
InnerText
物业HTML编码的内容,转向<p>
到<p>
如果要插入你需要使用HTML标签等等InnerHTML
.
双方innerText
并innerHTML
返回内部部件的HTML元素。
和之间innerText
innerHTML
的唯一区别是:innerText
将 HTML 元素(整个代码)作为字符串返回并在屏幕上显示 HTML 元素(作为 HTML 代码),而innerHTML
仅返回 HTML 元素的测试内容。
查看下面的示例以更好地理解。运行下面的代码。
const ourstring = 'My name is <b class="name">Satish chandra Gupta</b>.';
document.getElementById('innertext').innerText = ourstring;
document.getElementById('innerhtml').innerHTML = ourstring;
Run Code Online (Sandbox Code Playgroud)
.name {
color:red;
}
Run Code Online (Sandbox Code Playgroud)
<p><b>Inner text below. It inject string as it is into the element.</b></p>
<p id="innertext"></p>
<br />
<p><b>Inner html below. It renders the string into the element and treat as part of html document.</b></p>
<p id="innerhtml"></p>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
274010 次 |
最近记录: |