为什么更改按钮文本在 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>
提前感谢您的回答!
编辑 I. (0:40)
我忘了提一下,修改后查询按钮的值得到的结果似乎工作正常,这意味着它更改了 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>
编辑二。 (4:41)
但我还必须提到,它确实适用于“按钮”类型的输入元素 - 所以我用这样的元素补充了上面的代码。我还标记了哪些类型有效,哪些无效。
编辑三。
同时,我测试了它,它在 Opera 中不起作用 <= 11.11 , 但此错误已在 Opera 11.50 中修复虽然。
最佳答案
这是 Opera 中的一个错误。我不确定是什么原因导致的,但是重命名按钮的简单测试页面不会触发任何问题,但是在 @Darin 的 jsfiddle.net 示例中确实出现了错误。
这似乎是一个重绘错误。我注意到按钮的宽度发生变化以匹配新标签,但实际标签并没有改变。此外,离开页面并返回时,会显示新标签而不是旧标签。
我快速谷歌了一下,找不到其他人遇到过它。
这是我找到的解决方法:
$('#blahblah_button').val('there');
$('#blahblah_button').get(0).outerHTML = $('#blahblah_button').get(0).outerHTML;
也许有人可以找到更简洁的解决方法,最好是内置到 jQuery 的 val()
方法中的解决方法。但最好的解决方案显然是让 Opera 修复 bug。你应该给他们发一封电子邮件。
关于javascript - 对于 <input type ="submit"/> 元素,使用 JavaScript 更改按钮文本在 Opera (11.11) 中不起作用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084766/