怎么将html单选的内容复制到script里

html-css09

怎么将html单选的内容复制到script里,第1张

将这一段代码保存到一个文件中。 2、注意,您可以任何文本工具创建js文件,不过您在保存的时候,需要将名字后缀名为

.js

。 3、这里将js文件保存为

index.js

。之后如果我们需要使用

index.js

。我们只需要调用即可,不需要再写新的js文件。 4、在html的head部分引入js文件,具体的代码。 5、这个引用是一个双标签,就算没有内容,也得写全。 6、如果您的js是从外面引入,那么您不能在<script></script>中间再写js代码。 7、如果有需要,那么您需要重新声明script标签,在标签内写您的js代码。 8、无论是javascript jquery都是一样的规则,必须在script标签内写代码。引入了js文件后,您可以在需要的地方直接调用即可。

要用js实现

      var clipboard = function (window, document, navigator) {

          var textArea, copy

          function isOS() {

              return navigator.userAgent.match(/ipad|iphone/i)

          }

          function createTextArea(text) {

              textArea = document.createElement('textArea')

              textArea.value = text

              document.body.appendChild(textArea)

          }

          function selectText() {

              var range, selection

              if (isOS()) {

                  range = document.createRange()

                  range.selectNodeContents(textArea)

                  selection = window.getSelection()

                  selection.removeAllRanges()

                  selection.addRange(range)

                  textArea.setSelectionRange(0, 999999)

              } else {

                  textArea.select()

              }

          }

          function copyToClipboard() {

              document.execCommand('copy')

              document.body.removeChild(textArea)

          }

          copy = function copy(text) {

              createTextArea(text)

              selectText()

              copyToClipboard()

          }

          return {

              copy: copy

          }

      }(window, document, navigator)

用法:

clipboard.copy('222')

/1、网页代码如下:

<script language=javascript>

   // 自动 COPY 代码开始

   function MM_findObj(n, d) { //v4.0

   var p,i,x if(!d) d=document if((p=n.indexOf("?"))>0&&parent.frames.length) {

   d=parent.frames[n.substring(p+1)].document n=n.substring(0,p)}

   if(!(x=d[n])&&d.all) x=d.all[n] for (i=0!x&&i<d.forms.lengthi++) x=d.forms[n]

   for(i=0!x&&d.layers&&i<d.layers.lengthi++) x=MM_findObj(n,d.layers.document)

   if(!x && document.getElementById) x=document.getElementById(n) return x

   }

   function JM_cc(ob){

   var obj=MM_findObj(ob) if (obj) {

   obj.select()js=obj.createTextRange()js.execCommand("Copy")}

   alert("复制成功!您可以将本页推荐给您QQ或者论坛上的朋友阅读!")

   }

   

   // 自动 COPY 代码结束

   document.write('<br><b><font color=red style=font-size:14px>点击复制推荐给您的朋友阅读:</font></b><input name="page_url" value="'+window.location.href+'" size="64" style=color:red value="点击复制地址给您的朋友阅读" onClick=JM_cc("page_url")>')

   </script>

2、复制的HTML代码的话,可以在代码前加上<span>,在代码后加上</span>。例:<span>这里是你复制的内容!!</span>。