如何使用 Javascript 动态更改工具提示的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33881203/
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-10-28 16:59:46  来源:igfitidea点击:

How can I dynamically change the text of a tooltip with Javascript?

javascript

提问by Shelloid

I'm new to Javascript, and wish to change the text of a tooltip with Javascript.

我是 Javascript 的新手,希望使用 Javascript 更改工具提示的文本。

I've had a search around the questions but can't seem to find anything illuminating.

我对这些问题进行了搜索,但似乎找不到任何有启发性的东西。

Basically I need to get the tooltip text then replace it with something else, without disturbing the underlying HTML.

基本上我需要获取工具提示文本,然后将其替换为其他内容,而不会干扰底层 HTML。

Thanks heaps.

谢谢堆。

回答by collapsar

Assuming that elidis the ID of the element carrying the tooltip:

假设这elid是带有工具提示的元素的 ID:

document.getElementById('elid').setAttribute('title', '_new content_');

回答by AtheistP3ace

Quick example, I assume you mean the title attribute?

快速示例,我假设您的意思是标题属性?

Fiddle: http://jsfiddle.net/AtheistP3ace/wmzLs7gp/1/

小提琴:http: //jsfiddle.net/AtheistP3ace/wmzLs7gp/1/

HTML:

HTML:

<a title="some text" id="blah">hi</a>
<button id="mybutton">change tooltip</button>

JS:

JS:

document.getElementById('mybutton').addEventListener('click',
    function () {
        // These following two lines are the part that matter
        var anchor = document.getElementById('blah');
        anchor.title = 'new tooltip';
    }
);

回答by Luke Stoward

So this is dependant on where you are getting the data from that you wish to replace the tooltip text with. However you can replace the tooltip text as follows:

因此,这取决于您从何处获取要替换工具提示文本的数据。但是,您可以按如下方式替换工具提示文本:

You will need to reference the tooltip element in javascript. Next you need to set the text that you wish to change.

您需要在 javascript 中引用工具提示元素。接下来,您需要设置要更改的文本。

Basic selectors in javascript look like this

javascript 中的基本选择器如下所示

var myElement = document.getElementById('yourElementId');

See the example JSFiddlethat will change the text as soon as you run. Hopefully this should provide a basis on how to make the change.

请参阅示例JSFiddle,它将在您运行时立即更改文本。希望这应该为如何进行更改提供基础。

回答by Mike Sav

I'm unsure what you mean my Tooltip, I'm assuming you mean the altattribute on an image or the titleattribute you get some other HTML tags, with such lack of information I'd recommend looking into the setAttributemethod, for example

我不确定你的意思是我的工具提示,我假设你的意思alt是图像上的title属性或你得到一些其他 HTML 标签的属性,由于缺乏信息,我建议查看该setAttribute方法,例如

document.getElementById("myIdReference").setAttribute("alt", "My new Alt text");

There's more info here...

这里有更多信息...

http://www.w3schools.com/jsref/met_element_setattribute.asphttps://developer.mozilla.org/en/docs/Web/API/Element/setAttribute

http://www.w3schools.com/jsref/met_element_setattribute.asp https://developer.mozilla.org/en/docs/Web/API/Element/setAttribute