简体   繁体   English

Clipboard.js,其中所需的复制文本div与页面上的其他div具有相同的类或ID

[英]clipboard.js where the desired copied text div will have same class or id as other divs on page

I have multiple buttons that when clicked, should copy the text from a specific div. 我有多个按钮,当单击它们时,应从特定的div复制文本。 However, because the divs have IDs that are repeated on the page several times (due to being in a loop), I am unable to make sure the text that is copied is from the closest div with that ID. 但是,由于div的ID在页面上重复了几次(由于处于循环中),因此我无法确保复制的文本来自具有该ID的最近的div。

I am using Clipboard.js, but it is not a requirement if I can get the same functionality with a normal function. 我正在使用Clipboard.js,但是如果我能获得与普通功能相同的功能,则不是必需的。

My HTML code looks like this so far... but remember, it's in a dynamically-generated loop. 到目前为止,我的HTML代码看起来是这样的……但是请记住,它处于动态生成的循环中。 so "shortcodesTable" and each "shortcode1" div will be repeated. 因此,将重复“ shortcodesTable”和每个“ shortcode1” div。

<div id="shortcodesTable">
    <h4>Short Codes</h4>
        <div>
            <h5>Shortcode 1</h5>
            <button class="copy-btn" data-clipboard-target="#shortcode1">Copy</button>
            <div id="shortcode1"><pre><code> ... ... ... </code></pre></div>
        </div>
        <div>
            <h5>Shortcode 2</h5>
            <button class="copy-btn" data-clipboard-target="#shortcode2">Copy</button>
            <div id="shortcode2"><pre><code> ... ... ... </code></pre></div>
        </div>

and my JS is the basic function from the clipboard.js documentation: 我的JS是剪贴板.js文档中的基本功能:

var clipboard = new Clipboard('.copy-btn');

clipboard.on('success', function(e) {
        console.info('Copied Text:', e.text);

        e.clearSelection();
    });

    clipboard.on('error', function(e) {
        console.error('Action:', e.action);
        console.error('Trigger:', e.trigger);
    });

For more clarification, my code is looping through an api of credit cards and populating content for each one. 为了进一步说明,我的代码遍历了信用卡的api,并为每张卡填充内容。 This table that needs the copy functionality is within that loop. 需要复制功能的表位于该循环内。 It looks like this 看起来像这样

for (var i = 0; i < arrayLength; i++) {
    var 1 = blah;
    var 2 = blah2;

    $('<div>'+
        '<div>'+
           var 1 +
        '</div>' +
        '<div>'+
           var 2 +
        '</div>' +
        '<div id="shortcodesTable">'+
           '<h4>Short Codes</h4>'+
           '<div>'+
              '<h5>Shortcode 1</h5>'+
              '<button class="copy-btn" data-clipboard-target="#shortcode1">Copy</button>'+
              '<div id="shortcode1"><pre><code> ... ... ... </code></pre></div>'+
           '</div>'+
           '<div>'+
              '<h5>Shortcode 2</h5>'+
              '<button class="copy-btn" data-clipboard-target="#shortcode2">Copy</button>'+
              '<div id="shortcode2"><pre><code> ... ... ... </code></pre></div>'+
           '</div>'+
        '</div>'+
      '</div>').appendTo('.some-div');
}

You could use a counter variable or simply the index of your loop to give your elements unique ids. 您可以使用计数器变量,也可以仅使用循环索引为元素赋予唯一的ID。

As the W3 states: 如W3所述:

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. 该值在元素的主子树中的所有ID中必须唯一,并且必须至少包含一个字符。 The value must not contain any space characters. 该值不得包含任何空格字符。 https://www.w3.org/TR/html5/dom.html#the-id-attribute https://www.w3.org/TR/html5/dom.html#the-id-attribute

Working example using template strings 使用模板字符串的工作示例

function createShortcodeTables(iterations, root) {
  let html = ''
  for (let i = 0; i < iterations; i++) {
    html += `
      <div id="shortcodesTable-${i}">
      <h4>Short Codes</h4>
      <div>
        <h5>Shortcode 1</h5>
        <button class="copy-btn" data-clipboard-target="#shortcode-${i}">Copy</button>
        <div id="shortcode-${i}"><pre><code> Text of shortcode #${i} </code></pre></div>
      </div>
      <div>
        <h5>Shortcode 2</h5>
        <button class="copy-btn" data-clipboard-target="#shortcode-${i}-${i}">Copy</button>
        <div id="shortcode-${i}-${i}"><pre><code> Text of shortcode #${i}-${i} </code></pre></div>
      </div>
    </div>
    `
  }
  root.innerHTML = html
}

const rootElement = document.querySelector('.root')

createShortcodeTables(10, rootElement)

const clipboard = new Clipboard('.copy-btn');

clipboard.on('success', function(e) {
  console.info('Copied Text:', e.text);

  e.clearSelection();
});

clipboard.on('error', function(e) {
  console.error('Action:', e.action);
  console.error('Trigger:', e.trigger);
});

Example on JSFiddle JSFiddle上的示例

Edit: 编辑:

... +
'<button class="copy-btn" data-clipboard-target="#shortcode1">Copy</button>' +
'<div id="shortcode1"><pre><code> ... ... ... </code></pre></div>' + ...

in your data-clipboard-target and the corresponding id attributes you have to add your index variable i . data-clipboard-target和相应的id属性中,您必须添加索引变量i To do that you have to close your string and concatenate your index variable to it, otherwise it would print you the literal character i . 为此,您必须关闭字符串并将其索引变量连接到该字符串,否则它将打印出文字字符i

... +
'<button class="copy-btn" data-clipboard-target="#shortcode-' + i + '">Copy</button>' +
'<div id="shortcode-' + i + '"><pre><code> ... ... ... </code></pre></div>' + ...

Note the single quotes I've added inside your HTML attributes to close the string literal. 请注意我在HTML属性中添加的单引号,以关闭字符串文字。

More conceptual example: 更概念性的示例:

var arr = [1, 2, 3, 4, 5];
for (var index = 0; index < arr.length; index++) {
  console.log('<div id="some-id-' + index + '"></div>');
}

Working JSFiddle with string concatenation. 使用字符串连接工作JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM