未捕获的TypeError:无法读取未定义的属性“setData”

时间:2016-03-09 12:17:21

标签: javascript c# html asp.net

我编写了将数据从文本框复制到剪贴板的Javascript:

文本框:

function CopyToClipBoard()
{
    var text = document.getElementById('contNoTxtBox').innerHTML;                       
    window.clipboardData.setData('Text', text);                       
}

Javascript:

<input id="CopyButton" type="button" value="Copy" onclick="CopyToClipBoard()" /><br />

我用以下方式在HTML按钮中调用它:

     If is not Page.autopostback then
    'do something

else if is not MyDropdownlist.autopostback then    
    ' do something different    
    End if

但是我收到以下错误:

  

未捕获的TypeError:无法读取未定义的属性'setData'

为什么我得到这种类型的pf错误?

2 个答案:

答案 0 :(得分:2)

参考http://help.dottoro.com/ljctuhrg.php您的

  

window.clipboardData.setData('Text',text);

仅与 InternetExplorer 兼容。 还有其他方法可以使用Chrome等示例来实现这一点。

答案 1 :(得分:0)

Aspx code:
<head>
 <script language="javascript" type="text/javascript">

               function pasteContent() {
                   document.getElementById('ClipboardContent').value = document.execCommand('copy');//     window.clipboardData.getData('Text');
                   return (true);
               }
               var elem3 = document.getElementById("elem");
               document.addEventListener("click", function () {
                   document.getElementById("demo").innerHTML = "Hello World";
               });
               elem3.addEventListener('copy', function (e) {

                   e.preventDefault();
                   if (e.clipboardData) {
                       e.clipboardData.setData('text/plain', 'custom content from click');
                   } else if (window.clipboardData) {
                       window.clipboardData.setData('Text', 'custom content from click');
                   }

               });
               </script>    <script runat="server">
               protected override void OnInit(EventArgs e) { Page.ClientTarget =    "uplevel"; base.OnInit(e);
               } 
    </script>

</head>
 <body>

                <asp:Button ID="ReloadCtl" runat="server" Text="Paste" OnClick="ReloadCtl_Click"
                    OnClientClick="return pasteContent();" />
         <asp:HiddenField ID="ClipboardContent" runat="server" ClientIDMode="Static" /> </body>
    C# Code:
         protected void ReloadCtl_Click(object sender, EventArgs e)
                {
        if (!string.IsNullOrEmpty(this.ClipboardContent.Value))
                    {

        DataTable dt1 = DataMapper.GetDataTable(this.ClipboardContent.Value, false);
        }


    }