HTML5入门
<ul style="color: rgba(205, 91, 69, 1); font-size: 13px"><li>HTML5的初步认识</li>
<li></li>
</ul>
<h2 style="background-color: rgba(205, 91, 69, 1); border-radius: 5px"> 一、HTML5的初步认识</h2>
<p>关于html5的一些历史可以通过这篇博客了解:https://www.cnblogs.com/fly_dragon/archive/2012/05/22/2513716.html</p>
<h4>1.为HTML5建立的一些规则:</h4>
<ul style="color: rgba(205, 91, 69, 1); font-size: 13px">
<li>新特性应该基于HTML、CSS、DOM以及JavaScript</li>
<li>减少对外部插件的需求(比如flash)</li>
<li>更优秀的错误处理</li>
<li>更多取代脚本的标记</li>
<li>HTML5应该独立于设备</li>
<li>开发进程应对公众透明</li>
</ul>
<h4>2.HTML5新特性</h4>
<ul style="color: rgba(205, 91, 69, 1); font-size: 13px">
<li>用于绘画的canvas元素svg</li>
<li>用于媒介回放的video和audio元素</li>
<li>对本地离线存储的更好的支持:localStorage、sessionStorage</li>
<li>新的特殊内容元素,比如article、footer、header、nav、section</li>
<li>新的表单控件,比如calendar、date、time、email、url、search()</li>
<li>input type = text button file radio checkbox(html)</li>
</ul>
<p>新的的特殊内容元素:article、footer、header、nav、section本质上与div没什么区别,只是为了更加方便的阅读代码而产生的语义化标签:(article与section的区别一个相当于div一个相当于p)</p>
<div class="cnblogs_code">
<pre><header>头部、首部标签</header>
<nav>导航</nav>
<article>文章(div)</article>
<section>章节(p)</section>
<footer>底部</footer></pre>
</div>
<p>在HTML5中还允许自定义标签,自定义的标签属于行级元素,类似span。</p>
<div class="cnblogs_code">
<pre><student></student><span></span></pre>
</div>
<h4>2.HTML5新属性</h4>
<ul style="color: rgba(205, 91, 69, 1); font-size: 13px">
<li>Contenteditable用户能否修改页面上的内容</li>
<li>Draggable支持拖放</li>
<li>Hidden隐藏</li>
<li>Contextmenu为元素设定快捷菜单(目前无浏览器支持contextmenu属性)</li>
<li>data-val自定义属性</li>
</ul>
<p> 2.1contenteditable的属性值为boolean类型,当值为true表示当前元素内容可以编辑,默认为false不能编辑:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"><div contenteditable="true">这个div可以编辑:</div>
</pre>
</div>
<p> 实现效果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1309608/201907/1309608-20190729163037618-317631189.gif"></p>
<p>以下是基于contenteditable属性实现的富文本编辑器的代码:</p>
<div class="cnblogs_code"><img id="code_img_closed_1fa3d9a9-3f11-41ee-a204-9ba2396adefb" class="code_img_closed lazyload" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_1fa3d9a9-3f11-41ee-a204-9ba2396adefb" class="code_img_opened lazyload" style="display: none" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_1fa3d9a9-3f11-41ee-a204-9ba2396adefb" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <!DOCTYPE html>
<span style="color: rgba(0, 128, 128, 1)">2</span> <html>
<span style="color: rgba(0, 128, 128, 1)">3</span> <head>
<span style="color: rgba(0, 128, 128, 1)">4</span> <meta charset="utf-8">
<span style="color: rgba(0, 128, 128, 1)">5</span> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<span style="color: rgba(0, 128, 128, 1)">6</span> <title>富文本编辑器【Rich Text Editor】</title>
<span style="color: rgba(0, 128, 128, 1)">7</span> <link rel="stylesheet" href="">
<span style="color: rgba(0, 128, 128, 1)">8</span> <script type="text/javascript">
<span style="color: rgba(0, 128, 128, 1)">9</span> <span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> oDoc,sDefTxt;
</span><span style="color: rgba(0, 128, 128, 1)"> 10</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> initDoc(){
</span><span style="color: rgba(0, 128, 128, 1)"> 11</span> oDoc = document.getElementById("textBox"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 12</span> sDefTxt =<span style="color: rgba(0, 0, 0, 1)"> oDoc.innerHTML;
</span><span style="color: rgba(0, 128, 128, 1)"> 13</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(document.compForm.switchMode.checked){
</span><span style="color: rgba(0, 128, 128, 1)"> 14</span> setDocMode(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 15</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 16</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 17</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> formatDoc(sCmd, sValue){
</span><span style="color: rgba(0, 128, 128, 1)"> 18</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(validateMode()){
</span><span style="color: rgba(0, 128, 128, 1)"> 19</span> document.execCommand(sCmd, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">, sValue);
</span><span style="color: rgba(0, 128, 128, 1)"> 20</span> <span style="color: rgba(0, 0, 0, 1)"> oDoc.focus();
</span><span style="color: rgba(0, 128, 128, 1)"> 21</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 22</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 23</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> validateMode(){
</span><span style="color: rgba(0, 128, 128, 1)"> 24</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">document.compForm.switchMode.checked){
</span><span style="color: rgba(0, 128, 128, 1)"> 25</span> <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 26</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 27</span> alert("Uncheck\"Show Html\"."<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 28</span> <span style="color: rgba(0, 0, 0, 1)"> oDoc.focus();
</span><span style="color: rgba(0, 128, 128, 1)"> 29</span> <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 30</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 31</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> setDocMode(bToSource){
</span><span style="color: rgba(0, 128, 128, 1)"> 32</span> <span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> oContent;
</span><span style="color: rgba(0, 128, 128, 1)"> 33</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(bToSource){
</span><span style="color: rgba(0, 128, 128, 1)"> 34</span> oContent =<span style="color: rgba(0, 0, 0, 1)"> document.createTextNode(oDoc.innerHTML);
</span><span style="color: rgba(0, 128, 128, 1)"> 35</span> oDoc.innerHTML = ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 36</span> <span style="color: rgba(0, 0, 255, 1)">var</span> oPre = document.createElement("pre"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 37</span> oDoc.contentEditable = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 38</span> oPre.id = "sourceText"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 39</span> oPre.contentEditable = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 40</span> <span style="color: rgba(0, 0, 0, 1)"> oPre.appendChild(oContent);
</span><span style="color: rgba(0, 128, 128, 1)"> 41</span> <span style="color: rgba(0, 0, 0, 1)"> oDoc.appendChild(oPre);
</span><span style="color: rgba(0, 128, 128, 1)"> 42</span> document.execCommand("defaultParagraphSeparator", <span style="color: rgba(0, 0, 255, 1)">false</span>, "div"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 43</span> }<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 44</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(document.all){
</span><span style="color: rgba(0, 128, 128, 1)"> 45</span> oDoc.innerHTML =<span style="color: rgba(0, 0, 0, 1)"> oDoc.innerText;
</span><span style="color: rgba(0, 128, 128, 1)"> 46</span> }<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 47</span> oContent =<span style="color: rgba(0, 0, 0, 1)"> document.createRange();
</span><span style="color: rgba(0, 128, 128, 1)"> 48</span> <span style="color: rgba(0, 0, 0, 1)"> oContent.selectNodeContents(oDoc.firstChild);
</span><span style="color: rgba(0, 128, 128, 1)"> 49</span> oDoc.innerHTML =<span style="color: rgba(0, 0, 0, 1)"> oContent.toString();
</span><span style="color: rgba(0, 128, 128, 1)"> 50</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 51</span> oDoc.contentEditable = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 52</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 53</span> <span style="color: rgba(0, 0, 0, 1)"> oDoc.focus();
</span><span style="color: rgba(0, 128, 128, 1)"> 54</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 55</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> printDoc(){
</span><span style="color: rgba(0, 128, 128, 1)"> 56</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">validateMode()){
</span><span style="color: rgba(0, 128, 128, 1)"> 57</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ;
</span><span style="color: rgba(0, 128, 128, 1)"> 58</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 59</span> <span style="color: rgba(0, 0, 255, 1)">var</span> oPrntWin = windowl.open("","_blank","width=450,height=470,left=400,top=100,menubar=yes,toolbar=no,location=no,scrollbars=yes"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 60</span> <span style="color: rgba(0, 0, 0, 1)"> oPrntWin.document.open();
</span><span style="color: rgba(0, 128, 128, 1)"> 61</span> oPrntWin.document.write("<!doctype html><html><title>Print<\/title><\/head><body onload=\"print();\">" + oDoc.innerHTML + "<\/body><\/html>"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 62</span> <span style="color: rgba(0, 0, 0, 1)"> oPrntWin.document.close();
</span><span style="color: rgba(0, 128, 128, 1)"> 63</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 64</span> </script>
<span style="color: rgba(0, 128, 128, 1)"> 65</span> <style type="text/css">
<span style="color: rgba(0, 128, 128, 1)"> 66</span> <span style="color: rgba(0, 0, 0, 1)"> .intLink {
</span><span style="color: rgba(0, 128, 128, 1)"> 67</span> <span style="color: rgba(0, 0, 0, 1)"> cursor: pointer;
</span><span style="color: rgba(0, 128, 128, 1)"> 68</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 69</span> <span style="color: rgba(0, 0, 0, 1)"> img.intLink {
</span><span style="color: rgba(0, 128, 128, 1)"> 70</span> border: 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 71</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 72</span> <span style="color: rgba(0, 0, 0, 1)"> #toolBar1 select {
</span><span style="color: rgba(0, 128, 128, 1)"> 73</span> font-<span style="color: rgba(0, 0, 0, 1)">size:10px;
</span><span style="color: rgba(0, 128, 128, 1)"> 74</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 75</span> <span style="color: rgba(0, 0, 0, 1)"> #textBox {
</span><span style="color: rgba(0, 128, 128, 1)"> 76</span> <span style="color: rgba(0, 0, 0, 1)"> width: 540px;
</span><span style="color: rgba(0, 128, 128, 1)"> 77</span> <span style="color: rgba(0, 0, 0, 1)"> height: 200px;
</span><span style="color: rgba(0, 128, 128, 1)"> 78</span> border: 1px #000000<span style="color: rgba(0, 0, 0, 1)"> solid;
</span><span style="color: rgba(0, 128, 128, 1)"> 79</span> <span style="color: rgba(0, 0, 0, 1)"> padding: 12px;
</span><span style="color: rgba(0, 128, 128, 1)"> 80</span> <span style="color: rgba(0, 0, 0, 1)"> overflow: scroll;
</span><span style="color: rgba(0, 128, 128, 1)"> 81</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 82</span> <span style="color: rgba(0, 0, 0, 1)"> #textBox #sourceText {
</span><span style="color: rgba(0, 128, 128, 1)"> 83</span> padding: 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 84</span> margin: 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 85</span> min-<span style="color: rgba(0, 0, 0, 1)">width: 498px;
</span><span style="color: rgba(0, 128, 128, 1)"> 86</span> min-<span style="color: rgba(0, 0, 0, 1)">height: 200px;
</span><span style="color: rgba(0, 128, 128, 1)"> 87</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 88</span> <span style="color: rgba(0, 0, 0, 1)"> #editMode label {
</span><span style="color: rgba(0, 128, 128, 1)"> 89</span> <span style="color: rgba(0, 0, 0, 1)"> cursor: pointer;
</span><span style="color: rgba(0, 128, 128, 1)"> 90</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 91</span> </style>
<span style="color: rgba(0, 128, 128, 1)"> 92</span> </head>
<span style="color: rgba(0, 128, 128, 1)"> 93</span> <body onload="initDoc();">
<span style="color: rgba(0, 128, 128, 1)"> 94</span> <form name="compForm" method="post" action="sample.php" onsubmit="if(validateMode()){this.myDoc.value=oDoc.innerHTML;return true;}return false;">
<span style="color: rgba(0, 128, 128, 1)"> 95</span> <input type="hidden" name="myDoc">
<span style="color: rgba(0, 128, 128, 1)"> 96</span> <div id="toolBar1">
<span style="color: rgba(0, 128, 128, 1)"> 97</span> <select onchange="formatDoc('formatblock',this.value);this.selectedIndex=0;">
<span style="color: rgba(0, 128, 128, 1)"> 98</span> <option selected>- formatting -</option>
<span style="color: rgba(0, 128, 128, 1)"> 99</span> <option value="h1">Title 1 &lt;h1&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">100</span> <option value="h2">Title 2 &lt;h2&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">101</span> <option value="h3">Title 3 &lt;h3&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">102</span> <option value="h4">Title 4 &lt;h4&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">103</span> <option value="h5">Title 5 &lt;h5&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">104</span> <option value="h6">Subtitle &lt;h6&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">105</span> <option value="p">Paragraph &lt;p&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">106</span> <option value="pre">Preformatted &lt;pre&gt;</option>
<span style="color: rgba(0, 128, 128, 1)">107</span> </select>
<span style="color: rgba(0, 128, 128, 1)">108</span> <select onchange="formatDoc('fontname',this.value);this.selectedIndex=0;">
<span style="color: rgba(0, 128, 128, 1)">109</span> <option class="heading" selected>- font -</option>
<span style="color: rgba(0, 128, 128, 1)">110</span> <option>Arial</option>
<span style="color: rgba(0, 128, 128, 1)">111</span> <option>Arial Black</option>
<span style="color: rgba(0, 128, 128, 1)">112</span> <option>Courier New</option>
<span style="color: rgba(0, 128, 128, 1)">113</span> <option>Times New Roman</option>
<span style="color: rgba(0, 128, 128, 1)">114</span> </select>
<span style="color: rgba(0, 128, 128, 1)">115</span> <select onchange="formatDoc('fontsize',this.value);this.selectedIndex=0;">
<span style="color: rgba(0, 128, 128, 1)">116</span> <option class="heading" selected>- size -</option>
<span style="color: rgba(0, 128, 128, 1)">117</span> <option value="1">Very small</option>
<span style="color: rgba(0, 128, 128, 1)">118</span> <option value="2">A bit small</option>
<span style="color: rgba(0, 128, 128, 1)">119</span> <option value="3">Normal</option>
<span style="color: rgba(0, 128, 128, 1)">120</span> <option value="4">Medium-large</option>
<span style="color: rgba(0, 128, 128, 1)">121</span> <option value="5">Big</option>
<span style="color: rgba(0, 128, 128, 1)">122</span> <option value="6">Very big</option>
<span style="color: rgba(0, 128, 128, 1)">123</span> <option value="7">Maximum</option>
<span style="color: rgba(0, 128, 128, 1)">124</span> </select>
<span style="color: rgba(0, 128, 128, 1)">125</span> <select onchange="formatDoc('forecolor',this.value);this.selectedIndex=0;">
<span style="color: rgba(0, 128, 128, 1)">126</span> <option class="heading" selected>- color -</option>
<span style="color: rgba(0, 128, 128, 1)">127</span> <option value="red">Red</option>
<span style="color: rgba(0, 128, 128, 1)">128</span> <option value="blue">Blue</option>
<span style="color: rgba(0, 128, 128, 1)">129</span> <option value="green">Green</option>
<span style="color: rgba(0, 128, 128, 1)">130</span> <option value="black">Black</option>
<span style="color: rgba(0, 128, 128, 1)">131</span> </select>
<span style="color: rgba(0, 128, 128, 1)">132</span> <select onchange="formatDoc('backcolor',this.value);this.selectedIndex=0;">
<span style="color: rgba(0, 128, 128, 1)">133</span> <option class="heading" selected>- background -</option>
<span style="color: rgba(0, 128, 128, 1)">134</span> <option value="red">Red</option>
<span style="color: rgba(0, 128, 128, 1)">135</span> <option value="green">Green</option>
<span style="color: rgba(0, 128, 128, 1)">136</span> <option value="black">Black</option>
<span style="color: rgba(0, 128, 128, 1)">137</span> </select>
<span style="color: rgba(0, 128, 128, 1)">138</span> <div id="toolBar2">
<span style="color: rgba(0, 128, 128, 1)">139</span> <img class="intLink" title="Clean" onclick="if(validateMode()&&confirm('Are you sure?')){oDoc.innerHTML=sDefTxt};" src="data:image/gif;base64,R0lGODlhFgAWAIQbAD04KTRLYzFRjlldZl9vj1dusY14WYODhpWIbbSVFY6O7IOXw5qbms+wUbCztca0ccS4kdDQjdTLtMrL1O3YitHa7OPcsd/f4PfvrvDv8Pv5xv///////////////////yH5BAEKAB8ALAAAAAAWABYAAAV84CeOZGmeaKqubMteyzK547QoBcFWTm/jgsHq4rhMLoxFIehQQSAWR+Z4IAyaJ0kEgtFoLIzLwRE4oCQWrxoTOTAIhMCZ0tVgMBQKZHAYyFEWEV14eQ8IflhnEHmFDQkAiSkQCI2PDC4QBg+OAJc0ewadNCOgo6anqKkoIQA7" />
<span style="color: rgba(0, 128, 128, 1)">140</span> <img class="intLink" title="Print" onclick="printDoc();" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oEBxcZFmGboiwAAAAIdEVYdENvbW1lbnQA9syWvwAAAuFJREFUOMvtlUtsjFEUx//n3nn0YdpBh1abRpt4LFqtqkc3jRKkNEIsiIRIBBEhJJpKlIVo4m1RRMKKjQiRMJRUqUdKPT71qpIpiRKPaqdF55tv5vvusZjQTjOlseUkd3Xu/3dPzusC/22wtu2wRn+jG5So/OCDh8ycMJDflehMlkJkVK7KUYN+ufzA/RttH76zaVocDptRxzQtNi3mRWuPc+6cKtlXZ/sddP2uu9uXlmYXZ6Qm8v4Tz8lhF1H+zDQXt7S8oLMXtbF4e8QaFHjj3kbP2MzkktHpiTjp9VH6iHiA+whtAsX5brpwueMGdONdf/2A4M7ukDs1JW662+XkqTkeUoqjKtOjm2h53YFL15pSJ04Zc94wdtibr26fXlC2mzRvBccEbz2kiRFD414tKMlEZbVGT33+qCoHgha81SWYsew0r1uzfNylmtpx80pngQQ91LwVk2JGvGnfvZG6YcYRAT16GFtW5kKKfo1EQLtfh5Q2etT0BIWF+aitq4fDbk+ImYo1OxvGF03waFJQvBCkvDffRyEtxQiFFYgAZTHS0zwAGD7fG5TNnYNTp8/FzvGwJOfmgG7GOx0SAKKgQgDMgKBI0NJGMEImpGDk5+WACEwEd0ywblhGUZ4Hw5OdUekRBLT7DTgdEgxACsIznx8zpmWh7k4rkpJcuHDxCul6MDsmmBXDlWCH2+XozSgBnzsNCEE4euYV4pwCpsWYPW0UHDYBKSWu1NYjENDReqtKjwn2+zvtTc1vMSTB/mvev/WEYSlASsLimcOhOBJxw+N3aP/SjefNL5GePZmpu4kG7OPr1+tOfPyUu3BecWYKcwQcDFmwFKAUo90fhKDInBCAmvqnyMgqUEagQwCoHBDc1rjv9pIlD8IbVkz6qYViIBQGTJPx4k0XpIgEZoRN1Da0cij4VfR0ta3WvBXH/rjdCufv6R2zPgPH/e4pxSBCpeatqPrjNiso203/5s/zA171Mv8+w1LOAAAAAElFTkSuQmCC">
<span style="color: rgba(0, 128, 128, 1)">141</span> <img class="intLink" title="Undo" onclick="formatDoc('undo');" src="data:image/gif;base64,R0lGODlhFgAWAOMKADljwliE33mOrpGjuYKl8aezxqPD+7/I19DV3NHa7P///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARR8MlJq7046807TkaYeJJBnES4EeUJvIGapWYAC0CsocQ7SDlWJkAkCA6ToMYWIARGQF3mRQVIEjkkSVLIbSfEwhdRIH4fh/DZMICe3/C4nBQBADs=" />
<span style="color: rgba(0, 128, 128, 1)">142</span> <img class="intLink" title="Redo" onclick="formatDoc('redo');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAB1ChDljwl9vj1iE34Kl8aPD+7/I1////yH5BAEKAAcALAAAAAAWABYAAANKeLrc/jDKSesyphi7SiEgsVXZEATDICqBVJjpqWZt9NaEDNbQK1wCQsxlYnxMAImhyDoFAElJasRRvAZVRqqQXUy7Cgx4TC6bswkAOw==" />
<span style="color: rgba(0, 128, 128, 1)">143</span> <img class="intLink" title="Remove formatting" onclick="formatDoc('removeFormat')" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oECQMCKPI8CIIAAAAIdEVYdENvbW1lbnQA9syWvwAAAuhJREFUOMtjYBgFxAB501ZWBvVaL2nHnlmk6mXCJbF69zU+Hz/9fB5O1lx+bg45qhl8/fYr5it3XrP/YWTUvvvk3VeqGXz70TvbJy8+Wv39+2/Hz19/mGwjZzuTYjALuoBv9jImaXHeyD3H7kU8fPj2ICML8z92dlbtMzdeiG3fco7J08foH1kurkm3E9iw54YvKwuTuom+LPt/BgbWf3//sf37/1/c02cCG1lB8f//f95DZx74MTMzshhoSm6szrQ/a6Ir/Z2RkfEjBxuLYFpDiDi6Af///2ckaHBp7+7wmavP5n76+P2ClrLIYl8H9W36auJCbCxM4szMTJac7Kza////R3H1w2cfWAgafPbqs5g7D95++/P1B4+ECK8tAwMDw/1H7159+/7r7ZcvPz4fOHbzEwMDwx8GBgaGnNatfHZx8zqrJ+4VJBh5CQEGOySEua/v3n7hXmqI8WUGBgYGL3vVG7fuPK3i5GD9/fja7ZsMDAzMG/Ze52mZeSj4yu1XEq/ff7W5dvfVAS1lsXc4Db7z8C3r8p7Qjf///2dnZGxlqJuyr3rPqQd/Hhyu7oSpYWScylDQsd3kzvnH738wMDzj5GBN1VIWW4c3KDon7VOvm7S3paB9u5qsU5/x5KUnlY+eexQbkLNsErK61+++VnAJcfkyMTIwffj0QwZbJDKjcETs1Y8evyd48toz8y/ffzv//vPP4veffxpX77z6l5JewHPu8MqTDAwMDLzyrjb/mZm0JcT5Lj+89+Ybm6zz95oMh7s4XbygN3Sluq4Mj5K8iKMgP4f0////fv77//8nLy+7MCcXmyYDAwODS9jM9tcvPypd35pne3ljdjvj26+H2dhYpuENikgfvQeXNmSl3tqepxXsqhXPyc666s+fv1fMdKR3TK72zpix8nTc7bdfhfkEeVbC9KhbK/9iYWHiErbu6MWbY/7//8/4//9/pgOnH6jGVazvFDRtq2VgiBIZrUTIBgCk+ivHvuEKwAAAAABJRU5ErkJggg==">
<span style="color: rgba(0, 128, 128, 1)">144</span> <img class="intLink" title="Bold" onclick="formatDoc('bold');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAInhI+pa+H9mJy0LhdgtrxzDG5WGFVk6aXqyk6Y9kXvKKNuLbb6zgMFADs=" />
<span style="color: rgba(0, 128, 128, 1)">145</span> <img class="intLink" title="Italic" onclick="formatDoc('italic');" src="data:image/gif;base64,R0lGODlhFgAWAKEDAAAAAF9vj5WIbf///yH5BAEAAAMALAAAAAAWABYAAAIjnI+py+0Po5x0gXvruEKHrF2BB1YiCWgbMFIYpsbyTNd2UwAAOw==" />
<span style="color: rgba(0, 128, 128, 1)">146</span> <img class="intLink" title="Underline" onclick="formatDoc('underline');" src="data:image/gif;base64,R0lGODlhFgAWAKECAAAAAF9vj////////yH5BAEAAAIALAAAAAAWABYAAAIrlI+py+0Po5zUgAsEzvEeL4Ea15EiJJ5PSqJmuwKBEKgxVuXWtun+DwxCCgA7" />
<span style="color: rgba(0, 128, 128, 1)">147</span> <img class="intLink" title="Left align" onclick="formatDoc('justifyleft');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JMGELkGYxo+qzl4nKyXAAAOw==" />
<span style="color: rgba(0, 128, 128, 1)">148</span> <img class="intLink" title="Center align" onclick="formatDoc('justifycenter');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIfhI+py+0Po5y02ouz3jL4D4JOGI7kaZ5Bqn4sycVbAQA7" />
<span style="color: rgba(0, 128, 128, 1)">149</span> <img class="intLink" title="Right align" onclick="formatDoc('justifyright');" src="data:image/gif;base64,R0lGODlhFgAWAID/AMDAwAAAACH5BAEAAAAALAAAAAAWABYAQAIghI+py+0Po5y02ouz3jL4D4JQGDLkGYxouqzl43JyVgAAOw==" />
<span style="color: rgba(0, 128, 128, 1)">150</span> <img class="intLink" title="Numbered list" onclick="formatDoc('insertorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAADljwliE35GjuaezxtHa7P///////yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKSespwjoRFvggCBUBoTFBeq6QIAysQnRHaEOzyaZ07Lu9lUBnC0UGQU1K52s6n5oEADs=" />
<span style="color: rgba(0, 128, 128, 1)">151</span> <img class="intLink" title="Dotted list" onclick="formatDoc('insertunorderedlist');" src="data:image/gif;base64,R0lGODlhFgAWAMIGAAAAAB1ChF9vj1iE33mOrqezxv///////yH5BAEAAAcALAAAAAAWABYAAAMyeLrc/jDKSesppNhGRlBAKIZRERBbqm6YtnbfMY7lud64UwiuKnigGQliQuWOyKQykgAAOw==" />
<span style="color: rgba(0, 128, 128, 1)">152</span> <img class="intLink" title="Quote" onclick="formatDoc('formatblock','blockquote');" src="data:image/gif;base64,R0lGODlhFgAWAIQXAC1NqjFRjkBgmT9nqUJnsk9xrFJ7u2R9qmKBt1iGzHmOrm6Sz4OXw3Odz4Cl2ZSnw6KxyqO306K63bG70bTB0rDI3bvI4P///////////////////////////////////yH5BAEKAB8ALAAAAAAWABYAAAVP4CeOZGmeaKqubEs2CekkErvEI1zZuOgYFlakECEZFi0GgTGKEBATFmJAVXweVOoKEQgABB9IQDCmrLpjETrQQlhHjINrTq/b7/i8fp8PAQA7" />
<span style="color: rgba(0, 128, 128, 1)">153</span> <img class="intLink" title="Delete indentation" onclick="formatDoc('outdent');" src="data:image/gif;base64,R0lGODlhFgAWAMIHAAAAADljwliE35GjuaezxtDV3NHa7P///yH5BAEAAAcALAAAAAAWABYAAAM2eLrc/jDKCQG9F2i7u8agQgyK1z2EIBil+TWqEMxhMczsYVJ3e4ahk+sFnAgtxSQDqWw6n5cEADs=" />
<span style="color: rgba(0, 128, 128, 1)">154</span> <img class="intLink" title="Add indentation" onclick="formatDoc('indent');" src="data:image/gif;base64,R0lGODlhFgAWAOMIAAAAADljwl9vj1iE35GjuaezxtDV3NHa7P///////////////////////////////yH5BAEAAAgALAAAAAAWABYAAAQ7EMlJq704650B/x8gemMpgugwHJNZXodKsO5oqUOgo5KhBwWESyMQsCRDHu9VOyk5TM9zSpFSr9gsJwIAOw==" />
<span style="color: rgba(0, 128, 128, 1)">155</span> <img class="intLink" title="Hyperlink" onclick="var sLnk=prompt('Write the URL here','http:\/\/');if(sLnk&&sLnk!=''&&sLnk!='http://'){formatDoc('createlink',sLnk)}" src="data:image/gif;base64,R0lGODlhFgAWAOMKAB1ChDRLY19vj3mOrpGjuaezxrCztb/I19Ha7Pv8/f///////////////////////yH5BAEKAA8ALAAAAAAWABYAAARY8MlJq7046827/2BYIQVhHg9pEgVGIklyDEUBy/RlE4FQF4dCj2AQXAiJQDCWQCAEBwIioEMQBgSAFhDAGghGi9XgHAhMNoSZgJkJei33UESv2+/4vD4TAQA7" />
<span style="color: rgba(0, 128, 128, 1)">156</span> <img class="intLink" title="Cut" onclick="formatDoc('cut');" src="data:image/gif;base64,R0lGODlhFgAWAIQSAB1ChBFNsRJTySJYwjljwkxwl19vj1dusYODhl6MnHmOrpqbmpGjuaezxrCztcDCxL/I18rL1P///////////////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAVu4CeOZGmeaKqubDs6TNnEbGNApNG0kbGMi5trwcA9GArXh+FAfBAw5UexUDAQESkRsfhJPwaH4YsEGAAJGisRGAQY7UCC9ZAXBB+74LGCRxIEHwAHdWooDgGJcwpxDisQBQRjIgkDCVlfmZqbmiEAOw==" />
<span style="color: rgba(0, 128, 128, 1)">157</span> <img class="intLink" title="Copy" onclick="formatDoc('copy');" src="data:image/gif;base64,R0lGODlhFgAWAIQcAB1ChBFNsTRLYyJYwjljwl9vj1iE31iGzF6MnHWX9HOdz5GjuYCl2YKl8ZOt4qezxqK63aK/9KPD+7DI3b/I17LM/MrL1MLY9NHa7OPs++bx/Pv8/f///////////////yH5BAEAAB8ALAAAAAAWABYAAAWG4CeOZGmeaKqubOum1SQ/kPVOW749BeVSus2CgrCxHptLBbOQxCSNCCaF1GUqwQbBd0JGJAyGJJiobE+LnCaDcXAaEoxhQACgNw0FQx9kP+wmaRgYFBQNeAoGihCAJQsCkJAKOhgXEw8BLQYciooHf5o7EA+kC40qBKkAAAGrpy+wsbKzIiEAOw==" />
<span style="color: rgba(0, 128, 128, 1)">158</span> <img class="intLink" title="Paste" onclick="formatDoc('paste');" src="data:image/gif;base64,R0lGODlhFgAWAIQUAD04KTRLY2tXQF9vj414WZWIbXmOrpqbmpGjudClFaezxsa0cb/I1+3YitHa7PrkIPHvbuPs+/fvrvv8/f///////////////////////////////////////////////yH5BAEAAB8ALAAAAAAWABYAAAWN4CeOZGmeaKqubGsusPvBSyFJjVDs6nJLB0khR4AkBCmfsCGBQAoCwjF5gwquVykSFbwZE+AwIBV0GhFog2EwIDchjwRiQo9E2Fx4XD5R+B0DDAEnBXBhBhN2DgwDAQFjJYVhCQYRfgoIDGiQJAWTCQMRiwwMfgicnVcAAAMOaK+bLAOrtLUyt7i5uiUhADs=" />
<span style="color: rgba(0, 128, 128, 1)">159</span> </div>
<span style="color: rgba(0, 128, 128, 1)">160</span> <div id="textBox" contenteditable="true"><p>Lorem ipsum</p></div>
<span style="color: rgba(0, 128, 128, 1)">161</span> <p id="editMode"><input type="checkbox" name="switchMode" id="switchBox" onchange="setDocMode(this.checked);" /> <label for="switchBox">Show HTML</label></p>
<span style="color: rgba(0, 128, 128, 1)">162</span> <p><input type="submit" value="Send" /></p>
<span style="color: rgba(0, 128, 128, 1)">163</span> </form>
<span style="color: rgba(0, 128, 128, 1)">164</span> </body>
<span style="color: rgba(0, 128, 128, 1)">165</span> </html></pre>
</div>
<span class="cnblogs_code_collapse">富文本编辑器代码</span></div>
<p>2.2关于Draggable属性实现元素拖拽:</p>
<h4> 2.2.1属性:draggable: auto | true | false</h4>
<h4> 2.2.2拖拽事件:</h4>
<ul style="color: rgba(205, 91, 69, 1); font-size: 13px">
<li>dragstart 在元素开始被拖动时触发</li>
<li>dragend 在拖动操作完成时触发</li>
<li>drag 在元素被拖动时触发</li>
</ul>
<h4> 2.2.3释放区域事件:</h4>
<ul style="color: rgba(205, 91, 69, 1); font-size: 13px">
<li>dragenter 被拖动元素进入到释放区所占据得屏幕空间时触发</li>
<li>dragover 当被拖动元素在释放区内移动时触发</li>
<li>dragleave 当被拖动元素没有放下就离开释放区时触发</li>
<li>drop 当被拖动元素在释放区里放下时触发</li>
</ul>
<div class="cnblogs_code"><img id="code_img_closed_034695a4-d459-4ae3-8456-44c170fe3b4c" class="code_img_closed lazyload" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_034695a4-d459-4ae3-8456-44c170fe3b4c" class="code_img_opened lazyload" style="display: none" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_034695a4-d459-4ae3-8456-44c170fe3b4c" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <!DOCTYPE HTML>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <html>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <head>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <meta charset="utf-8">
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <title>菜鸟教程(runoob.com)</title>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <style>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">.droptarget {
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">float</span><span style="color: rgba(0, 0, 0, 1)">: left;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)"> width: 100px;
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)"> height: 35px;
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)"> margin: 15px;
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)"> padding: 10px;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)"> border: 1px solid #aaaaaa;
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">15</span> </style>
<span style="color: rgba(0, 128, 128, 1)">16</span> </head>
<span style="color: rgba(0, 128, 128, 1)">17</span> <body>
<span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> <p>在两个矩形框中来回拖动 p 元素:</p>
<span style="color: rgba(0, 128, 128, 1)">20</span> <div class="droptarget">
<span style="color: rgba(0, 128, 128, 1)">21</span> <p draggable="true" id="dragtarget">拖动我!</p>
<span style="color: rgba(0, 128, 128, 1)">22</span> </div>
<span style="color: rgba(0, 128, 128, 1)">23</span> <div class="droptarget"></div>
<span style="color: rgba(0, 128, 128, 1)">24</span> <p style="clear:both;"><strong>注意:</strong>Internet Explorer 8 及更早 IE 版本或 Safari 5.1 及更早版本的浏览器不支持 drag 事件。</p>
<span style="color: rgba(0, 128, 128, 1)">25</span> <p id="demo"></p>
<span style="color: rgba(0, 128, 128, 1)">26</span> <script>
<span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 拖动时触发</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">28</span> document.addEventListener("dragstart", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">dataTransfer.setData()方法设置数据类型和拖动的数据</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)"> console.log(event.dataTransfer);
</span><span style="color: rgba(0, 128, 128, 1)">31</span> event.dataTransfer.setData("Text"<span style="color: rgba(0, 0, 0, 1)">, event.target.id);
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 拖动 p 元素时输出一些文本</span>
<span style="color: rgba(0, 128, 128, 1)">33</span> document.getElementById("demo").innerHTML = "开始拖动 p 元素."<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改拖动元素的透明度</span>
<span style="color: rgba(0, 128, 128, 1)">35</span> event.target.style.opacity = "0.4"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">36</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在拖动p元素的同时,改变输出文本的颜色</span>
<span style="color: rgba(0, 128, 128, 1)">38</span> document.addEventListener("drag", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">39</span> document.getElementById("demo").style.color = "red"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当拖完p元素输出一些文本元素和重置透明度</span>
<span style="color: rgba(0, 128, 128, 1)">42</span> document.addEventListener("dragend", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">43</span> document.getElementById("demo").innerHTML = "完成 p 元素的拖动"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">44</span> event.target.style.opacity = "1"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">45</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 拖动完成后触发 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当p元素完成拖动进入droptarget,改变div的边框样式</span>
<span style="color: rgba(0, 128, 128, 1)">48</span> document.addEventListener("dragenter", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ( event.target.className == "droptarget"<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">50</span> event.target.style.border = "3px dotted red"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">51</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">52</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">53</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认情况下,数据/元素不能在其他元素中被拖放。对于drop我们必须防止元素的默认处理</span>
<span style="color: rgba(0, 128, 128, 1)">54</span> document.addEventListener("dragover", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">55</span> <span style="color: rgba(0, 0, 0, 1)"> event.preventDefault();
</span><span style="color: rgba(0, 128, 128, 1)">56</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">57</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当可拖放的p元素离开droptarget,重置div的边框样式</span>
<span style="color: rgba(0, 128, 128, 1)">58</span> document.addEventListener("dragleave", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">59</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ( event.target.className == "droptarget"<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">60</span> event.target.style.border = ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">61</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">62</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">63</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">对于drop,防止浏览器的默认处理数据(在drop中链接是默认打开)
</span><span style="color: rgba(0, 128, 128, 1)">64</span> <span style="color: rgba(0, 128, 0, 1)">复位输出文本的颜色和DIV的边框颜色
</span><span style="color: rgba(0, 128, 128, 1)">65</span> <span style="color: rgba(0, 128, 0, 1)">利用dataTransfer.getData()方法获得拖放数据
</span><span style="color: rgba(0, 128, 128, 1)">66</span> <span style="color: rgba(0, 128, 0, 1)">拖拖的数据元素id(“drag1”)
</span><span style="color: rgba(0, 128, 128, 1)">67</span> <span style="color: rgba(0, 128, 0, 1)">拖拽元素附加到drop元素</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">68</span> document.addEventListener("drop", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {
</span><span style="color: rgba(0, 128, 128, 1)">69</span> <span style="color: rgba(0, 0, 0, 1)"> event.preventDefault();
</span><span style="color: rgba(0, 128, 128, 1)">70</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ( event.target.className == "droptarget"<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">71</span> document.getElementById("demo").style.color = ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">72</span> event.target.style.border = ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">73</span> <span style="color: rgba(0, 0, 255, 1)">var</span> data = event.dataTransfer.getData("Text"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">74</span> <span style="color: rgba(0, 0, 0, 1)"> event.target.appendChild(document.getElementById(data));
</span><span style="color: rgba(0, 128, 128, 1)">75</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">76</span> <span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 128, 1)">77</span> </script>
<span style="color: rgba(0, 128, 128, 1)">78</span>
<span style="color: rgba(0, 128, 128, 1)">79</span> </body>
<span style="color: rgba(0, 128, 128, 1)">80</span> </html></pre>
</div>
<span class="cnblogs_code_collapse">来自一个手册得完整示例</span></div>
<p> 》》示例链接:https://www.runoob.com/try/try.php?filename=tryjsref_ondrag_all</p>
<p>2.3Hidden实现的元素隐藏本质上就是基于display的样式属性none实现的,这个隐藏的元素依然存在于文档结构中,只是display属性被设置为none了而已。</p>
<p>2.4自定义属性:</p>
<div class="cnblogs_code">
<pre><div data-val="demo"></div>
document.getElementsByTagName('div').getAttribute('data-val');<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">demo</span></pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
——生命自会找到蓬勃之路。<br><br>
来源:https://www.cnblogs.com/ZheOneAndOnly/p/11264585.html
頁:
[1]