Javascript 单击按钮时的工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37798967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:44:18  来源:igfitidea点击:

Tooltip on click of a button

javascriptclipboard.js

提问by user5368737

I'm using clipboard.jsto copy some text from a textarea, and that's working fine, but I want to show a tooltip saying "Copied!" if it was successfully copied like they do in the example given on their website.

我正在使用clipboard.js从 a 复制一些文本textarea,这工作正常,但我想显示一个工具提示,上面写着“已复制!” 如果它像他们在他们网站上给出的例子中那样成功复制。

Here's an example of it working without showing a tooltip: https://jsfiddle.net/5j50jnhj/

这是它在不显示工具提示的情况下工作的示例:https: //jsfiddle.net/5j50jnhj/

回答by Zeno Rocha

Clipboard.js creator here. So Clipboard.js is not opinionated about user feedback which means it doesn't come with a tooltip solution.

Clipboard.js 创建者在这里。所以 Clipboard.js 对用户反馈并不固执己见,这意味着它没有提供工具提示解决方案。

But here's an example of how you can integrate it with Bootstrap's Tooltip.

但这里有一个示例,说明如何将其与 Bootstrap 的工具提示集成。

// Tooltip

$('button').tooltip({
  trigger: 'click',
  placement: 'bottom'
});

function setTooltip(message) {
  $('button').tooltip('hide')
    .attr('data-original-title', message)
    .tooltip('show');
}

function hideTooltip() {
  setTimeout(function() {
    $('button').tooltip('hide');
  }, 1000);
}

// Clipboard

var clipboard = new Clipboard('button');

clipboard.on('success', function(e) {
  setTooltip('Copied!');
  hideTooltip();
});

clipboard.on('error', function(e) {
  setTooltip('Failed!');
  hideTooltip();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<button class="btn btn-primary" data-clipboard-text="1">Click me</button>

回答by binard

I do it like it

我喜欢它

HTML :

HTML :

<button class="test" data-clipboard-text="1">Button 1</button>
<button class="test" data-clipboard-text="1">Button 2</button>

JS :

JS:

$('.test').tooltip({
  trigger: 'click',
  placement: 'bottom'
});

function setTooltip(btn, message) {
  btn.tooltip('hide')
    .attr('data-original-title', message)
    .tooltip('show');
}

function hideTooltip(btn) {
  setTimeout(function() {
    btn.tooltip('hide');
  }, 1000);
}

var clipboard = new Clipboard('.test');

clipboard.on('success', function(e) {
    var btn = $(e.trigger);
  setTooltip(btn, 'Copied');
  hideTooltip(btn);
});

With jsfiddle link https://jsfiddle.net/hs48sego/1/

使用 jsfiddle 链接https://jsfiddle.net/hs48sego/1/

回答by Aikon Mogwai

*[tooltip]:focus:after {
  content: attr(tooltip);
  display:block;
  position: absolute;    
}
<button tooltip="I'm tooltip">Button</button>
<br><br>
<a href = "javascript:void(0)" tooltip = "I'm tooltip">Link</a>

回答by Brendon Colburn

Here's a js fiddle that implements this the way the website does it, I stole the source code: https://jsfiddle.net/bmbs7yco/

这是一个js小提琴,它按照网站的方式实现了这一点,我偷了源代码:https: //jsfiddle.net/bmbs7yco/

the main components to the solution are:

该解决方案的主要组成部分是:

function showTooltip(elem, msg) {
    elem.setAttribute('class', 'btn tooltipped tooltipped-s');
    elem.setAttribute('aria-label', msg);
}

clipboard.on('success', function(e) {
  console.info('Action:', e.action);
  console.info('Text:', e.text);
  console.info('Trigger:', e.trigger);
    showTooltip(e.trigger, 'Copied!');
  e.clearSelection();
});

and adding their primer.css. A less lazy method would be to extract the classes from the css you need.

并添加他们的primer.css。一种不那么懒惰的方法是从您需要的 css 中提取类。

回答by Altynbek S.

This solution work, if you have some buttons and etc:

如果您有一些按钮等,此解决方案有效:

function setTooltip(e,message) {
  $(e.trigger).tooltip({
  trigger: 'click',
  placement: 'bottom'
});
$(e.trigger).tooltip('hide')
 .attr('data-original-title', message)
 .tooltip('show');
}

function hideTooltip(e) {
  setTimeout(function() {
    $(e.trigger).tooltip('hide');
  }, 1000);
}

// Clipboard

var clipboard = new Clipboard('button');

clipboard.on('success', function(e) {
  setTooltip(e,'Copied!');
  hideTooltip(e);
});

clipboard.on('error', function(e) {
  setTooltip(e,'Failed!');
  hideTooltip(e);
});

回答by wcb1

I am using Menucool JavaScript Tooltip. It leaves to the triggering element to decide how to launch the tooltip:

我正在使用 Menucool JavaScript 工具提示。由触发元素决定如何启动工具提示:

<span onclick="tooltip.pop(this, 'Hello world!')">
    Click me
</span>