javascript 更改 HTML 按钮的标题

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

Change Title of a HTML button

javascripthtmlhtmlbutton

提问by Amila

I want to Change the title of a HTML button once clicked. this is what I tried.

单击后,我想更改 HTML 按钮的标题。这是我试过的。

HTML

HTML

<button class="fullScreen" data-dojo-type="dijit.form.Button" onclick="FullScreenToggle(this);" id="butFullScreen">
<input type="image" class="ns" onclick="FullScreenToggle(this);" title="Full Screen" value="" />

JS

JS

if (document.getElementById("butFullScreen").title == "Full Screen") {
    document.getElementById("butFullScreen").title = "";
    document.getElementById("butFullScreen").title = "Normal Screen";
} else document.getElementById("butFullScreen").title = "Full Screen";

When I do this Title doesn't change in JavaScript call. then I tried using title=""in HTML, then title changes on Java Script call. but when button is loaded first time I dont have a title. I want have Full Screen as title when button is loaded and then toggle it between Normal Screen & Full Screen on each click.

当我这样做时,JavaScript 调用中的 Title 不会改变。然后我尝试title=""在 HTML 中使用,然后在 Java Script 调用中更改标题。但是当第一次加载按钮时,我没有标题。我希望在加载按钮时将全屏作为标题,然后在每次单击时在普通屏幕和全屏之间切换。

Suggestions are welcome, Thanks in Advance.

欢迎提出建议,提前致谢。

Update

更新

problem in brief is once I set title="Full Screen" in my HTML, java-script call doesn't change it. I there any solution or reasoning for that?

简而言之,问题是一旦我在 HTML 中设置了 title="Full Screen",java-script 调用就不会改变它。我有任何解决方案或理由吗?

Solved

解决了

kept the title="" as blank, and called the java script method in <body onloadmethod. Problem solved.

保持title=""为空,在<body onloadmethod中调用java脚本方法。问题解决了。

回答by Micha? Sza?apski

u need to call a function onload site Execute a JavaScript when a page is finished loading:

你需要调用一个函数 onload site 在页面加载完成后执行 JavaScript:

In HTML:

在 HTML 中:

<body onload="FullScreenToggle();">

In JavaScript:

在 JavaScript 中:

window.onload=function(){FullScreenToggle();};

回答by Kalyan

try this In button Click:

试试这个在按钮点击:

 var butn=document.getElementById("butFullScreen").value;
 if(butn=="Full Screen")
     document.getElementById("butFullScreen").value="Normal Screen";
 if(butn=="Normal Screen")
     document.getElementById("butFullScreen").value="Full Screen";

回答by Shadow

Try this

试试这个

if (document.getElementById("butFullScreen").getAttribute('title')=="Full Screen"){ 
   document.getElementById("butFullScreen").setAttribute('title',"Normal text");
    }
else
   document.getElementById("butFullScreen").setAttribute('title',"Full Screen");

回答by shi69

Give title to your button:

给你的按钮标题:

function FullScreenToggle(){
 if (document.getElementById("butFullScreen").title == "Full Screen"){
    document.getElementById("butFullScreen").title = "Normal Screen";
 } else {
    document.getElementById("butFullScreen").title = "Full Screen";
 } 

回答by Tony L.

My preference would be to make only 1 document.getElementByIdcall for cleanliness and ease of maintenance. Also, I would use the .titleattibute directly.

我的偏好是只打 1 次document.getElementById清洁和易于维护的电话。另外,我会.title直接使用属性。

function FullScreenToggle() {
    var button = document.getElementById("butFullScreen");
    if (button.title == "Full Screen") {
        button.title = "Normal Screen";
    } else {
        button.title = "Full Screen";
    }
}