为什么更改按钮文本在Opera 11.11中不适用于
等元素<input type="submit" value="Asdasd" id="blahblah_button" />
? (还没有在早期版本中尝试过。)
我尝试使用jQuery和“纯”JavaScript,但没有一个能够工作。
这是我试过的jQuery代码:
$('#blahblah_button').val('Blah-blah');
这是“纯粹的”JS代码:
document.getElementById('blahblah_button').value = 'Blah-blah';
为什么它们都没有在Opera 11.11中运行?它在IE,Chrome和FF中工作,令我惊讶的是它没有在Opera工作。
我必须提到它也适用于Opera中的按钮标签:
<button id="test_button" onclick="$(this).text('Blahblah');">Some text</button>
提前感谢您的回答!
我忘了提到在修改后查询按钮的值会得到结果它似乎工作正常,这意味着它改变了JS DOM中的结构,但没有适当地重新呈现可见按钮。
这是您可以尝试此行为的示例代码:
http://jsbin.com/inuxix/1/edit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="hu" xml:lang="hu">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Changing button text</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<p>Button tag - WORKING
<button onclick="$(this).text('Blahblah_1');" id="test_button">Button_text (button_tag)</button>
</p>
<p>Input tag (type: submit) - NOT working
<input onclick="$(this).val('Blahblah_2');" type="submit" value="Submit_text" id="blahblah_submit_type" />
</p>
<p>Input tag (type: button) - WORKING
<input onclick="$(this).val('Blahblah_3');" type="button" value="Button_text" id="blahblah_button_type" />
</p>
<p>
<button onclick="alert($('#blahblah_submit_type').val());" id="val_button">Getting blahblah_submit_type's value</button>
</p>
</body>
</html>
但我还必须提到它适用于带有“按钮”类型的输入元素 - 所以我用上面这样的元素补充了我的代码。我还标记了哪些类型有效,哪些无效。
与此同时,我对它进行了测试,但它在Opera&lt; = 11.11,中无效,但此错误已在Opera 11.50 中修复。< / p>
答案 0 :(得分:8)
这是Opera中的一个错误。我不确定是什么导致它,但是重命名按钮的简单测试页面不会触发任何问题,但是在@ Darin的jsfiddle.net示例中,错误确实出现了。
这似乎是一个重绘错误。我注意到按钮的宽度会更改以匹配新标签,但实际标签不会更改。此外,远离页面并返回,显示新标签而不是旧标签。
我做了一个快速的谷歌,找不到其他人遇到它。
以下是我找到的解决方法:
$('#blahblah_button').val('there');
$('#blahblah_button').get(0).outerHTML = $('#blahblah_button').get(0).outerHTML;
也许有人可以找到一个更清晰的解决方法,理想情况下是jQuery的val()
方法中的一个。但最好的解决方案显然是Opera修复了这个bug。你应该给他们发一封电子邮件。
答案 1 :(得分:6)
<html>
<input type="submit" value="A" id="blahblah" onClick="click_input(this)" />
<script>
function click_input(input)
{
input.value = "test";
input.type = "submit";
}
</script>
</html>
是的,这很奇怪,但我希望Opera的人很快就会解决这个问题。你能报告一下吗,顺便问一下?
答案 2 :(得分:1)
我找到了js的另一种方式:
document.getElementById('blahblah_button').innerText = "testText";