簡體   English   中英

cocoa WebView中innerhtml和outerhtml的區別

[英]Difference between innerhtml and outerhtml in cocoa WebView

我在我的應用程序中使用cocoa webview進行富文本編輯。 只是與webkit中可用的innerHtml和outerHtml方法混淆。

任何人都可以解釋有什么區別

[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerHTML];

[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerText];

innerHTML是DOM元素的一個屬性,表示元素內部的HTML,即開始和結束標記之間的HTML。 它已被廣泛復制,但實現方式各不相同(可能因為它沒有公布的標准[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

假設我們有一個用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為

標題為Your_Name

例如,如果我們在這種情況下采用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

我已經在示例的幫助下對其進行了解釋,其中這四個術語的定義已經在下面的答案中解釋過了。

 <!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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM