An Example Analysis of the Difference between innerHTML and pasteHTML in JS

  • 2021-06-29 10:07:22
  • OfStack

This article gives an example of the difference between innerHTML and pasteHTML in JS.Share it for your reference, as follows:

innerHTML is an attribute that gets or sets the HTML content within that element, and can be used by any element that can contain an HTML child node

pasteHTML() is a method that replaces text or HTML within a specified text area and must be applied to an area created by createTextRange() or document.selection.createRange().

Example:


<Script Language="JavaScript">
function addLink(){
 var oRange = document.selection.createRange();
 if(oRange.text!=''){
   var oUrl = window.prompt(' Link URL ...','http://www.163.com/');
   var oHtml = '<a href='+oUrl+' target=_blank>'+oRange.text+'</a>';
   oRange.pasteHTML(oHtml);
 }else{
   window.alert(' You did not choose to add linked text! ');
 }
}
</Script>
 NetEase    Sina    Sohu <br><br>
<input type="button" value=" Add Link " onclick="addLink();">

More readers interested in JavaScript-related content can view this site's topics: JavaScript Switching Special Effects and Techniques Summary, JavaScript Find Algorithmic Techniques Summary, JavaScript Animation Special Effects and Techniques Summary, JavaScript Errors and Debugging Techniques Summary, JavaScript Data Structure and Algorithmic Techniques Summary,Summary of JavaScript Traversal Algorithms and Techniques and Summary of JavaScript Mathematical Operation Usage

I hope that the description in this paper will be helpful to everyone's JavaScript program design.


Related articles: