我有这个有效的代码片段:
var oldInput = document.getElementsByName ("prio");
oldInput[0].setAttribute ("disabled", "disabled");
这是相关的目标页面 HTML(已更正):
<div id="content">
<p>(CONTENT)</p>
<form>
<p>Priority: <input name="prio" type="text" value="285"></p>
<p>Success: <input name="succ" type="text" value="6"></p>
</form>
</div>
<table border="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="user">
<a href="#" class="nav" title="Logout John">Logout</a>
(User: John)
</td>
</tr>
</tbody>
</table>
我有 1 个接入点供许多用户使用。但我希望 5 个用户能够更改 prio
值。您可以看到用户名在页脚中。
是否可以在没有 jQuery 的情况下使用 Firefox 2.x 的上述代码在 javascript 中做到这一点?
例子:
if USER (from footer) == JOHN || LUKE || JEFF || MAX || ANDY
do nothing
else
disable PRIO BOX (from content)
最佳答案
这应该在 Firefox 2.0 中工作:
//--- Make sure this list of names is all uppercase.
var usersWhoCanSetPriority = ['JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY'];
var bDisablePrio = true;
var tdNodes = document.getElementsByTagName ("TD");
for (var J = tdNodes.length - 1; J >= 0; --J) {
var tdNode = tdNodes[J];
if (tdNode.className == "user") {
var userName = tdNode.textContent.replace (
/^(?:.|\n|\r)+\(User:\s+([^)]+)\)(?:.|\n|\r)+$/i, "$1"
).toUpperCase ();
if (usersWhoCanSetPriority.indexOf (userName) > -1) {
bDisablePrio = false;
}
}
}
if (bDisablePrio) {
var oldInput = document.getElementsByName ("prio");
oldInput[0].setAttribute ("disabled", "disabled");
}
参见 the updated Fiddle .
关于javascript - 如何根据显示的用户文本禁用输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231164/