Home > Web Front-end > JS Tutorial > JavaScript converts hyperlinks in specified areas of web pages into URLs

JavaScript converts hyperlinks in specified areas of web pages into URLs

不言
Release: 2018-12-01 16:01:33
Original
4110 people have browsed it

This article will introduce to you how to use JavaScript converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs to convert hyperlinks in specified areas of web pages into URLs. Let’s take a look at the specific content.

JavaScript converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs

Without further ado, let’s look directly at the example below

The code is as follows

DropHyperLink.css

.dropArea {
    margin-top:8px;    
    margin-bottom:8px;    
    width:320px;    
    height:64px;    
    background-color:#fff2a7;    
    border: 1px solid #ff6a00;
}
.longTextBox{
    width:80%;
}
Copy after login

DropHyperLink.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" href="DropHyperLink.css" />
  <script type="text/javascript">
    function load() {
          var droparea = document.getElementById(&#39;LinkDropArea&#39;);
      droparea.addEventListener(&#39;dragover&#39;, onDragOver, false);
      droparea.addEventListener(&#39;drop&#39;, onDrop, false);
    }    
    function onDragOver(event) {      //event.stopPropagation();
      event.preventDefault();
      event.dataTransfer.dropEffect = &#39;link&#39;;
    }    
    function onDrop(event) {
          var url = event.dataTransfer.getData("text");      
          var textBox = document.getElementById(&#39;OutputTextBox&#39;);
      textBox.value = url;
    }  
  </script>
</head>
<body onload="load();">
  <div id="LinkDropArea" class="dropArea">把超链接拖到这里</div>
  <input id="OutputTextBox" class="longTextBox" type="text"/>
</body>
</html>
Copy after login

Description:

You can open the link of the web browser in the div box with ID = LinkDropArea. To accept a drop, handle the dragode event on the frame's element. During event handling, preventDefault() is called and prevents operations that do not receive a default drag. Also set it to dataTransfer.dropEffect. The drop event handler also calls the dataTransfer.getData("text") method to get the element's text data. In the case of links, the URL of the link is stored in the text data of the stored element. The string of the obtained URL will be displayed in the text box at the bottom of the screen.

Run results

JavaScript converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs

Then prepare another web browser window, drag the hyperlink in the page displayed on the web browser, and Drop it in the drop area on the page.

JavaScript converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs

For example, if you drag "Front-end Development", the result will be as shown below

JavaScript converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs converts hyperlinks in specified areas of web pages into URLs

The above is the detailed content of JavaScript converts hyperlinks in specified areas of web pages into URLs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template