Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onClick按钮最大字符长度_Javascript_Html - Fatal编程技术网

Javascript onClick按钮最大字符长度

Javascript onClick按钮最大字符长度,javascript,html,Javascript,Html,下午,我试图创建一个简单的HTML页面,上面有一些按钮,当你点击它们时,它会调用一个JS将一些文本复制到剪贴板上,这样它就可以粘贴到其他地方Word文档等 <body> <button onclick="setClipboard('Thank you for your help.')">Greeting</button> <button onclick="setClipboard('Lorem Ipsum is simply dummy

下午,我试图创建一个简单的HTML页面,上面有一些按钮,当你点击它们时,它会调用一个JS将一些文本复制到剪贴板上,这样它就可以粘贴到其他地方Word文档等

<body>
    <button onclick="setClipboard('Thank you for your help.')">Greeting</button>
    <button onclick="setClipboard('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s')">Item 2</button>
</body>
它可以复制第一个按钮没有问题,但第二个按钮不起作用。如果我缩短按钮2中的单词数量,它就会工作。 这让我觉得它不起作用,因为里面有太长/太多的单词


任何帮助都将不胜感激。

这是因为在第二个按钮中,文本有一个单数引号“正在中断字符串。您需要将第二个按钮的setClipboard中的起始引号和结束引号更改为backticks`以使其与onclick上的双引号和引号一起工作:

<button onclick="setClipboard(`Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s`)">Item 2</button>
反勾号是ECMAScript 6的一个显式特性;它们被称为模板文本,它们允许单引号和双引号驻留在字符串中。如果您的浏览器不支持ES6,则仅用反斜杠\转义单引号也可以


似乎第二个按钮内的文本需要使用\转义quoation标记“以使其正常工作,如下所示:

函数setClipboardvalue{ var tempInput=document.createElementinput; tempInput.style=位置:绝对;左侧:-1000px;顶部:-1000px; tempInput.value=值; document.body.appendChildtempInput; tempInput.select; 文件副本; document.body.removeChildtempInput; console.logvalue; } 招呼
Item 2Backticks在JS中不是有效的字符串分隔符,因此会引发错误…@CBroe它在ES6中是有效的字符串分隔符,它们称为模板文本。@CBroe如果需要证明,请参阅fiddle.Oops,我不知道。但您可能应该明确指出,这是ES6的一项功能,否则人们可能会想,为什么它不能在较旧的浏览器中工作。。。既然我们这里有一个OP,它甚至不知道“common”JS的绝对语法基础,我现在也不会对它们抛出ES6。
<button onclick="setClipboard(`Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s`)">Item 2</button>