问题描述
伙计,
我需要获取表单属性的内容。
在读/写模式下这是字段,所以我可以使用getElementById(" name")。value
在ReadOnly中,这只是纯文本,所以我可以使用
getElementById(" name")。innerText。
我需要动态地执行它,所以无论如何我可以查看getElementById()返回的
对象并确定它是否是一个字段或者一个
标签?
(我可以根据需要调用.value或.innerText)
TIA -Adam
Folks,
I need to get the contents of a form attribute.
In Read/Write mode this is field, so I can use getElementById("name").value
In ReadOnly this is just plain text, so I can use
getElementById("name").innerText.
I need to do it dynamically though, so is there anyway I can look at the
object returned by getElementById() and determine if it was a field or a
tag?
(I can then call ".value" or ".innerText" as appropriate)
TIA -Adam
推荐答案
微软的innerText没有W3C等价物(在IE之外支持的不是很好的b $ b),但取决于在您特定的
情况下,提供类似功能可能很简单。
-
Rob
There is no W3C equivalent for Microsoft''s innerText (which is not well
supported outside IE), however depending on your particular
circumstance, it may be simple to provide similar functionality.
--
Rob
`。 innerText''不合适,它是专有的,而getElementById()
是Web标准的一部分。你应该避免在没有测试的情况下混合两种。
参见< http://pointedears.de/scripts/test/whatami> ;,§2。
非-function属性不被调用(如方法所示),可以访问它们。
也许你正在寻找这个:
// don不使用document.get ...通过......()如果你不需要
var objectReference = document.forms [...]。elements [" name" ];
if(objectReference)
{
//输入表单控件
if( typeof objectReference.tagName!=" undefined"
&& objectReference.tagName.toLowerCase()==" input"
&& objectReference.type .toLowerCase()==" text")
{
... objectReference.value ...
}
//没有表单控件,W3C DOM Level 3
else if(typeof objectReference.textContent!=" undefined")
{
... objectReference.textContent。 ..
}
//没有表格控制; IE专有
else if(typeof objectReference.innerText!=" undefined")
{
... objectReference.innerText ...
}
HTH
PointedEars
`.innerText'' is not appropriate, it is proprietary, while getElementById()
is part of a Web standard. You should avoid mixing both without tests.
See <http://pointedears.de/scripts/test/whatami>, §2.
Non-function properties are not called (as methods are), they are accessed.
Maybe you are looking for this:
// don''t use document.get...By...() if you don''t have to
var objectReference = document.forms[...].elements["name"];
if (objectReference)
{
// input form control
if (typeof objectReference.tagName != "undefined"
&& objectReference.tagName.toLowerCase() == "input"
&& objectReference.type.toLowerCase() == "text")
{
... objectReference.value ...
}
// no form control, W3C DOM Level 3
else if (typeof objectReference.textContent != "undefined")
{
... objectReference.textContent ...
}
// no form control; IE proprietary
else if (typeof objectReference.innerText != "undefined")
{
... objectReference.innerText ...
}
HTH
PointedEars
这篇关于从字段或标记获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!