Jquery帮助 - OnClick将标题发送到剪贴板

时间:2016-01-16 00:14:37

标签: javascript jquery html

我是JavaScript / jQuery的新手。

我将以下行作为我从DB Query动态创建的表的一部分。我的最终目标是一个jQuery函数,当点击跨度时会将该跨度的title属性的内容发送到剪贴板,但到目前为止,我甚至无法找到值,所有我得到的都是未定义的当我点击任何跨度/列。

<tr>
    <td id="1AVIFilePath"><span style='color:Red;' onClick='copyToClipboard()' title='c:/FTP/DASales/Working/File/EVENT/1E03029B20160107101053001i100.avi' >False</span></td>
    <td id="1AVISuccess"><span style='color:Red;' onClick='copyToClipboard()' title='C:/TOS3/1E03029B20160107101053001i100.mp4'>False</span></td>
    <td id="1AVICopy"><span style='color:Red;' onClick='copyToClipboard()' title='E:/OriginalAvis/1E03029B20160107101053001i100.avi'>False</span></td>
    <td id="1AVIFail"><span style='color:Red;' onClick='copyToClipboard()' title='F:/AVIFailed/1E03029B20160107101053001i100.avi'>False</span></td>
    <td id="1AVIS3Loc"><span style='color:Red;' onClick='copyToClipboard()' title='s3://PathTo/File/1E03029B20160107101053001i100.avi'>False</span></td>
    <td id="1MP4S3Loc"><span style='color:Red;' onClick='copyToClipboard()' title='s3://PathTo/File/1E03029B20160107101053001i100.mp4'>False</span></td>
<tr>

<script>
    function copyToClipboard() {
        var str = $(this).attr('title');
        console.log(str);
    }
</script

1 个答案:

答案 0 :(得分:0)

您需要从span标记中删除onClick属性并设置事件侦听器,如下所示。注意:你需要一个jQuery on方法的选择器,所以我还在表行元素中添加了一个类名(这里称为&#34;可点击&#34;,但可以是任何东西)。

有关jQuery on方法的更多信息,请访问:

http://api.jquery.com/on/

&#13;
&#13;
function copyToClipboard() {
    var str = $(this).attr('title');
    console.log(str);
}

$(document).ready(function () {
    $('.clickable').on('click', 'span', copyToClipboard);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr class="clickable">
        <td id="1AVIFilePath"><span style='color:Red;' title='c:/FTP/DASales/Working/File/EVENT/1E03029B20160107101053001i100.avi' >False</span></td>
        <td id="1AVISuccess"><span style='color:Red;' title='C:/TOS3/1E03029B20160107101053001i100.mp4'>False</span></td>
        <td id="1AVICopy"><span style='color:Red;' title='E:/OriginalAvis/1E03029B20160107101053001i100.avi'>False</span></td>
        <td id="1AVIFail"><span style='color:Red;' title='F:/AVIFailed/1E03029B20160107101053001i100.avi'>False</span></td>
        <td id="1AVIS3Loc"><span style='color:Red;' title='s3://PathTo/File/1E03029B20160107101053001i100.avi'>False</span></td>
        <td id="1MP4S3Loc"><span style='color:Red;' title='s3://PathTo/File/1E03029B20160107101053001i100.mp4'>False</span></td>
    <tr>
</table>
&#13;
&#13;
&#13;