Home Web Front-end JS Tutorial 2 solutions to using outerHTML in Firefox_javascript tips

2 solutions to using outerHTML in Firefox_javascript tips

May 16, 2016 pm 04:45 PM
firefox

Everyone must be familiar with the innerHTML attribute of the DOM object, but the outerHTML is less useful. The innerHTML attribute returns the HTML contained in the DOM object from the start tag to the end tag, while the outerHTML attribute returns is the HTML including the tag of the DOM object itself. The following figure can well explain the difference between the two attributes:
2 solutions to using outerHTML in Firefox_javascript tips

outerHTML was originally a private attribute belonging to IE. You can view MSDN Description on: outerHTML Property(http://msdn.microsoft.com/en-us/library/ms534310(VS.85).aspx). Currently, IE, Chrome, Safari, and Opera all support this attribute. The problem is that outerHTML does not support Firefox. In Firefox, this attribute always returns undefined. Fortunately, HTML5 will add this attribute.
Let Firefox support the outerHTML attribute by extending the prototype of HTMLElement:

Copy code The code is as follows:

if (typeof(HTMLElement) != "undefined") {
HTMLElement.prototype.__defineSetter__("outerHTML", function(s) {
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var df = r .createContextualFragment(s);
this.parentNode.replaceChild(df, this);
return s;
});
HTMLElement.prototype.__defineGetter__("outerHTML", function(){
var a = this.attributes, str = "<" this.tagName, i = 0;
for (; i < a.length; i )
if (a[i].specified)
str = " " Hormis dans les machines a sous preferees universelles, les casinos offrent des jeux par exemple Grandes six roues, Pai Go Poker, Blackjack, Baccarat, la Roulette et le Craps, entre autres. a[i].name "="" a[i].value """;
if (!this.canHaveChildren)
return str " />";
return str ">" this.innerHTML "";
});

HTMLElement.prototype.__defineGetter__("canHaveChildren", function(){
return
!/^(area|base|basefont|
col|frame|hr|img|br|
input|isindex|link|meta
|param)$/.test(this.tagName.toLowerCase());
});
}



This method comes from W3Help (http://www.w3help.org/zh-cn/causes/SD9017), which is a bit cumbersome and requires intrusion into the prototype. There is a simpler alternative, first create an empty node, add the DOM object to obtain the outerHTML attribute to the empty node, and then access the innerHTML of the empty node:

Copy code The code is as follows:

function outerHtml(elem){
if(typeof elem === "string") elem = document.getElementById(elem);
// Create an empty div node
var div = document.createElement("div");
// Will copy elemCopy is inserted into an empty div node
div.appendChild(elem.cloneNode(true));
// Return the HTML content of the div
return div.innerHTML;
};

Compared with the above method, there is no need to touch the prototype, and the amount of code is much less. I believe there will be other solutions.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove Firefox Snap in Ubuntu Linux? How to remove Firefox Snap in Ubuntu Linux? Feb 21, 2024 pm 07:00 PM

To remove FirefoxSnap in Ubuntu Linux, you can follow these steps: Open a terminal and log in to your Ubuntu system as administrator. Run the following command to uninstall FirefoxSnap: sudosnapremovefirefox You will be prompted for your administrator password. Enter your password and press Enter to confirm. Wait for command execution to complete. Once completed, FirefoxSnap will be completely removed. Note that this will remove versions of Firefox installed via the Snap package manager. If you installed another version of Firefox through other means (such as the APT package manager), you will not be affected. Go through the above steps

Can mozilla firefox be uninstalled? Can mozilla firefox be uninstalled? Mar 15, 2023 pm 04:40 PM

Mozilla Firefox can be uninstalled; Firefox is a third-party browser and can be uninstalled if it is not needed. Uninstallation method: 1. In the Start menu, click "Windwos System" - "Control Panel"; 2. In the "Control Panel" interface, click "Programs and Features"; 3. In the new interface, find and double-click Firefox Browser icon; 4. In the uninstall pop-up window, click "Next"; 5. Click "Uninstall".

Firefox 113 new features: support for AV1 animations, enhanced password generator and picture-in-picture features Firefox 113 new features: support for AV1 animations, enhanced password generator and picture-in-picture features Mar 05, 2024 pm 05:20 PM

According to recent news, while Mozilla released the stable version of Firefox 112, it also announced that the next major version, Firefox 113, has entered the Beta channel and supports AV1 animations, enhanced password generator and picture-in-picture features. The main new functions/features of Firefox 113 are as follows: Support for AV1 format animated images (AVIS); Enhance the security of the password generator by introducing special characters; Enhance the picture-in-picture function, support rewind, display video time, and enable full screen more easily Mode provides official DEB installation files for Debian and Ubuntu distributions. Updated bookmark import feature, icons for imported bookmarks are supported by default. Hardware accelerated AV1 video decoding is enabled by default on supported hardware using w

How to use Mozilla Firefox in Scrapy to solve the problem of scanning QR code to log in? How to use Mozilla Firefox in Scrapy to solve the problem of scanning QR code to log in? Jun 22, 2023 pm 09:50 PM

For crawlers to crawl websites that require login, verification code or scan code login is a very troublesome problem. Scrapy is a very easy-to-use crawler framework in Python, but when processing verification codes or scanning QR codes to log in, some special measures need to be taken. As a common browser, Mozilla Firefox provides a solution that can help us solve this problem. The core module of Scrapy is twisted, which only supports asynchronous requests, but some websites require the use of cookies and

Firefox browser Firefox 115 released, supports the last version of Win7/Win8.1 Firefox browser Firefox 115 released, supports the last version of Win7/Win8.1 Mar 04, 2024 pm 04:46 PM

Today’s latest news, Mozilla officially released the Firefox 115 stable version update today. The most noteworthy thing about this update is that this is the last version that supports Win7/Win8, macOS10.12, 10.13 and 10.14. Download address: https://ftp.mozilla.org/pub/firefox/releases/115.0/Mozilla stated in the official update log: Microsoft will end support for Win7 and Win8 systems in January 2023, and Firefox 115 released today The version is the last version update received by users of said system. Win7 and Win8 users

Ubuntu 23.10 will run Firefox in native Wayland mode by default Ubuntu 23.10 will run Firefox in native Wayland mode by default Feb 29, 2024 am 10:10 AM

Canonical recently announced that in the upcoming Ubuntu 23.10, Firefox Snap has been configured to run in Wayland mode by default. Note: Ubuntu currently has the Wayland session by default, and Firefox can also work normally. However, currently FirefoxSnap actually runs in XWayland compatibility mode, rather than the strict native Wayland mode. Canonical announced that it will run the Firefox browser in Wayland mode by default, so that problems such as interface blur and scaling distortion will not occur on HiDPI displays, and it will support touch gestures such as dragging and pinching. As mentioned above, Ubunt

Firefox 115 Beta released: Introducing Quick Actions Firefox 115 Beta released: Introducing Quick Actions Mar 04, 2024 pm 03:10 PM

New news yesterday: While Mozilla released the Firefox 114 stable version update, it also shifted its development focus to the Firefox 115 version and launched the Beta version today. It is learned from reports that Firefox 115 introduces CookieBannerReduction and QuickActions buttons that were originally planned to be launched in version 114. After users enable CookieBannerReduction in Firefox version 115 and visit supported websites, the browser will automatically reject cookie requests on cookiebanners. The second feature is the QuickActions button on the address bar

How to solve the problem that Font Awesome is not displayed in Firefox under Apache/Nginx How to solve the problem that Font Awesome is not displayed in Firefox under Apache/Nginx May 21, 2023 pm 05:43 PM

1. nginx server solution The server uses nginx. To add the access-control-allow-origin field to the response header, the method of adding is to use the add_header directive: Configuration example: Copy the code as follows: location/assets/{gzip_staticon; expiresmax;add_headercache-controlpublic;add_headeraccess-control-allow-origin*;} 2. Apache server solution fontawesome (firefox cannot display fire

See all articles