我想在点击后更改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
if (document.getElementById("butFullScreen").title == "Full Screen") {
document.getElementById("butFullScreen").title = "";
document.getElementById("butFullScreen").title = "Normal Screen";
} else document.getElementById("butFullScreen").title = "Full Screen";
当我这样做时,标题在JavaScript调用中不会改变。然后我尝试在HTML中使用title=""
,然后在Java Script调用上更改标题。但是当第一次加载按钮时我没有标题。我希望在加载按钮时将全屏作为标题,然后在普通屏幕和正常屏幕之间切换。每次点击都会全屏显示。
欢迎提出建议,在此先感谢。
更新
问题很简单,一旦我在我的HTML中设置了title =“全屏”,java脚本调用就不会改变它。我有任何解决方案或推理吗?
解决了
将title =“”保留为空白,并在<body onload
方法中调用java脚本方法。问题解决了。
答案 0 :(得分:3)
你需要调用函数onload site 页面加载完成后执行JavaScript:
在HTML中:
<body onload="FullScreenToggle();">
在JavaScript中:
window.onload=function(){FullScreenToggle();};
答案 1 :(得分:3)
试试这个 在按钮中单击:
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";
答案 2 :(得分:2)
试试这个
if (document.getElementById("butFullScreen").getAttribute('title')=="Full Screen"){
document.getElementById("butFullScreen").setAttribute('title',"Normal text");
}
else
document.getElementById("butFullScreen").setAttribute('title',"Full Screen");
答案 3 :(得分:2)
为您的按钮添加标题:
function FullScreenToggle(){
if (document.getElementById("butFullScreen").title == "Full Screen"){
document.getElementById("butFullScreen").title = "Normal Screen";
} else {
document.getElementById("butFullScreen").title = "Full Screen";
}
答案 4 :(得分:0)
我的偏好是只需要document.getElementById
要求清洁和便于维护。另外,我会直接使用.title
attibute。
function FullScreenToggle() {
var button = document.getElementById("butFullScreen");
if (button.title == "Full Screen") {
button.title = "Normal Screen";
} else {
button.title = "Full Screen";
}
}