具有属性的多警报Javascript

时间:2017-03-01 07:16:48

标签: javascript jquery html

我有一个问题,当我尝试多重警报时,我看到所有结果都相同(所有数据都是" 1")。我想要:

  • 如果点击按钮number1 => alert:1
  • 如果点击按钮number2 =>警告:2
  • 如果点击按钮number3 =>警告:3

我附上了一张关于我的问题的照片

image



<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>constructor-selector</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <!-- 1. Define some markup -->
    <button class="btn" data-clipboard-text="1">Copy</button>
    <button class="btn" data-clipboard-text="2">Copy</button>
    <button class="btn" data-clipboard-text="3">Copy</button>

    <!-- 2. Include library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="../dist/clipboard.min.js"></script>

    <!-- 3. Instantiate clipboard by passing a string selector -->

	<script>
	var clipboard = new Clipboard('button');
	
	clipboard.on('success', function(e) {
		console.log(e);
		var test = $(".btn").attr("data-clipboard-text");
		alert (test + " : was copy on clipboard");
	});

	clipboard.on('error', function(e) {
		console.log(e);
	});
	</script>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

更改此行

var test = $(".btn").attr("data-clipboard-text");

用这个

var test = e.text;

实例化剪贴板并且copy事件成功后,事件的文本e.text基本上是点击按钮的data-clipboard-text属性的值。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.6.0/clipboard.min.js"></script>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>constructor-selector</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <!-- 1. Define some markup -->
    <button class="btn" data-clipboard-text="1">Copy</button>
    <button class="btn" data-clipboard-text="2">Copy</button>
    <button class="btn" data-clipboard-text="3">Copy</button>


    <!-- 3. Instantiate clipboard by passing a string selector -->

	<script>
    // instantiate Clipboard
	var clipboard = new Clipboard('button');
	// once text is copied to clipboard
	clipboard.on('success', function(e) {
        // event action "copy"
        // event text "e.text" now has the value of data-clipboard-text attribute
		//console.log(e);
		var test = e.text;
		alert (test + " : was copy on clipboard");
	});

	clipboard.on('error', function(e) {
		console.log(e);
	});
	</script>
</body>
</html>

答案 1 :(得分:0)

试试这个:

&#13;
&#13;
var clipboard = new Clipboard('.btn');
	
	clipboard.on('success', function(e) {
		console.log(e);
		var test = e.text;
		alert (test + " : was copy on clipboard");
	});

	clipboard.on('error', function(e) {
		console.log(e);
	});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.6.1/clipboard.min.js"></script>
<button class="btn" data-clipboard-text="1">Copy</button>
<button class="btn" data-clipboard-text="2">Copy</button>
<button class="btn" data-clipboard-text="3">Copy</button>
&#13;
&#13;
&#13;