Web series of tutorials

1. Compatible code

1. Encapsulate innerText and TextContent // Set the text content of any label to any content function setText(element, text) { (typeof element.TextContent === "undefined") ? (element.innerText = text) : (element.textContent = text); } // Get the text content of any tag function getText(element) { return (typeof element.TextContent === "undefined" ? element.innerText : element.textContent); } // sample code my$("btn").onclick = function () { setText(my$("dv"), "hahahhahha"); }; my$("btn").onclick = function () { console.log(getText(my$("dv"))); }; 1. Set the text content in the paired label: The innerText property is an IE8 standard property, supported by chrome, fireFox, and IE8. textContent is a W3C standard attribute, supported by chrome and firefox, but not in IE8. 2. Get the text content in the paired tags: In IE8, use textContent to get the text content in the paired tags, and the return value is undefined. Then it means that the types of properties that are not supported by browsers are undefined. Determine whether to use innerText or textContent by judging whether the element has a textContent property (if not, the element's textContent property is undefined). 2. innerText and innerHTML
Ha ha

p tag

innerText property: set and get only the text content. innerHTML property: Not only can you get the text content, but you can also set and get the html tag to display or get the format of the corresponding tag. 2. Custom attribute operations (set, get, remove)

p tag

1. The custom attribute in the tag cannot be obtained by means of DOM object.attribute, because this attribute does not exist in the DOM, forcibly obtained can only be undefined. 2. Correspondingly, the setting is the same. The custom label is set by means of DOM object.attribute. The result is only the custom attribute of the DOM object. This custom attribute will not be displayed on the label. Get: Get the value of the custom attribute through the DOM object .getAttibute("the name of the custom attribute"). Setting: Set the custom attribute through the DOM object .setAttibute("custom attribute name", "custom attribute value") . Remove: Set the custom attribute through the DOM object .removeAttibute("Name of the custom attribute") . PS: removeAttibute can also be used to remove attributes that come with elements, such as class attributes. removeAttribute("class")

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00