【问题标题】:Difference between innerhtml and outerhtml in cocoa WebView可可WebView中innerhtml和outerhtml的区别
【发布时间】:2012-12-24 17:35:28
【问题描述】:
我在我的应用程序中使用 cocoa webview 进行富文本编辑。只是与 webkit 中的 innerHtml 和 outerHtml 方法混淆了。
谁能解释一下有什么区别
[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerHTML];
与
[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerText];
【问题讨论】:
标签:
objective-c
macos
cocoa
webview
【解决方案1】:
<!DOCTYPE html>
<html>
<head>
<title>innerHTML and outerHTML | Javascript Usages</title>
</head>
<body>
<div id="replace">REPLACE By inner or outer HTML</div>
<script>
userwant = "inner";
userwant = "outer";
if (userwant = "inner") {
document.querySelector("#replace").innerHTML;
// this will remove just message: 'REPLACE By inner or outer HTML' //
} else if (userwant = "outer") {
document.querySelector("#replace").outerHTML;
// this will remove all element <div> ~ </div> by the message: 'REPLACE By inner or outer HTML' //
};
</script>
</body>
</html>
【解决方案2】:
假设我们有一个使用 html 加载到 webview 的页面
<html>
<head><title>Your Title</title></head>
<body>
<h1>Heading</h1>
<p id="para" >hi <b>Your_Name</b></p>
</body>
<html>
现在。
[(DOMHTMLElement *)[[webView mainFrame] DOMDocument] documentElement]
将返回 DOMHTMLElement "html" 和
outerHTML 将返回完整的 html 为
<html>
<head><title>Your Title</title></head>
<body>
<h1>Heading</hi>
<p id="para">hi <b>Your_Name</b></p>
</body>
<html>
outerText 将返回 html 为
标题
嗨你的名字
例如,如果我们在这种情况下以 p 标记为例
outerHTML will return - <p id="para">hi <b>Your_Name</b></p>
outerText will return - hi Your_Name
innerHTML will return - hi <b>Your_Name</b>
innerText will return - hi Your_Name
我已经在示例的帮助下进行了解释,其中这 4 个术语的定义已经在下面的答案中进行了解释。
【解决方案3】:
innerHTML 是表示 HTML 的 DOM 元素的属性
在元素内部,即在开始标签和结束标签之间。它有
被广泛复制,但实现有所不同(可能是因为它
没有公布的标准[1])特别是在他们如何对待元素
属性。
outerHTML 类似于innerHTML,它是一个元素属性
包括开始和结束标签以及内容。它
没有像 innerHTML 那样被广泛复制,所以它或多或少仍然存在
仅限 IE。
<p id="pid">welcome</p>
innerHTML of element "pid" == welcome
outerHTML of element "pid" == <p id="pid">welcome</p>
在哪里
innerText容器的文本内容。
outerText 访问读取时与 innerText 相同;分配新值时替换整个元素。
<p id="pid">welcome</p>
innerText of element "pid" == welcome
outerText of element "pid" == welcome