从字段或标记获取内容 [英] getting contents from field or tag

查看:49
本文介绍了从字段或标记获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,


我需要获取表单属性的内容。


在读/写模式下这是字段,所以我可以使用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


推荐答案




document.getElementById (name)。tagName


它返回标签的名称


干杯!


Jeje

Hi,

document.getElementById("name").tagName

it return the name of the tag

cheers!

Jeje


Adam Lipscombe写道:
Adam Lipscombe wrote:
人们,

我需要得到表单attr的内容ibute。

在读/写模式下这是字段,所以我可以使用getElementById(" name")。值
在ReadOnly中这只是纯文本,所以我可以使用
getElementById(" name")。innerText。


您需要提供一个小的HTML示例。一个readonly表单元素可以

仍然会给出一个显示在文本框中的值,但是用户

不能直接更改它,例如


< input type =" text" value =我只读 readonly

onclick =" alert(this.value);">

我需要动态地做,所以无论如何我可以看看
getElementById()返回的对象,并确定它是字段还是
标记?


getElementById()将返回对元素的引用,它永远不会是

''字段''或''tag''。


获得参考后,您可以访问元素的属性或属性

。如果您不确定您获得的元素是否具有值属性,请先测试它。您还可以通过查看nodeName来检查您找到的元素类型




var el = document.getElementById(...) ;

if(''INPUT''== el.nodeName){

//做输入元素处理......

}


你还应该记住,像选项这样的表格元素不一定有价值,例如


< ;选项> A< / option>

符合W3C标准的浏览器中上述选项元素的值是

文本''A'',但是IE的值将是未定义的,你必须明确地获得该选项的

文本属性。


像Firefox这样的浏览器会满意:


if(el.value){

//做一些值为

}的其他如果(el.text){

//用文字做一些事情

}否则{

//现在,价值和文字都是空的......

}


但不是IE。你必须更加努力并使用select的


属性:
$ b&b< input type =" text" value =我只读 readonly

onclick =" blurt(this)">

< select onchange =" blurt(this);">

<选项选项>< /选项>

<选项>选择1< /选项>

<选项价值="选项2的值" ;> opt 2< / option>

< option> opt 3< / option>

< / select>


< script type =" text / javascript">

function blurt(el){

if(''SELECT''== el.nodeName) {

if(el [el.selectedIndex] .value){

alert(el [el.selectedIndex] .value);

}否则if(el [el.selectedIndex] .text){

alert(el [el.selectedIndex] .text);

} else {

alert(''值和文字为空...'');

}

}否则if(''INPUT''== el.nodeName) {

if(''text''== el.type){

alert(el.value);

}

//为收音机,复选框等添加案例......

}

}

< / script>


您可能希望使用开关阻止处理各种情况而不是一堆ifs。

(然后我可以调用.value或.innerText或(视情况而定)
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.
You need to provide a small HTML example. A readonly form element can
still be given a value that is displayed in the text box, but the user
can''t change it directly, e.g.

<input type="text" value="I''m readonly" readonly
onclick="alert(this.value);">

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?
getElementById() will return a reference to an element, it is never a
''field'' or ''tag''.

Once you have a reference, you can access the attributes or properties
of the element. If you are unsure whether the element you get hold of
has a value attribute, then test it first. You can also check the type
of element you''ve found by looking at the nodeName:

var el = document.getElementById(...);
if ( ''INPUT'' == el.nodeName ) {
// do input element processing...
}

You should also remember that some form elements like options do not
necessarily have a value, e.g.

<option>A</option>

The value of the above option element in W3C compliant browsers is the
text ''A'', but in IE the value will be undefined, you have to get the
text attribute of the option explicitly.

Browsers like Firefox will be happy with:

if ( el.value ) {
// do something with the value
} else if ( el.text) {
// do something with the text
} else {
// What now, both value and text are empty...
}

But not IE. You must try a little harder and use the selectedIndex
property of the select:

<input type="text" value="I''m readonly" readonly
onclick="blurt(this)">
<select onchange="blurt(this);">
<option selected></option>
<option>opt 1</option>
<option value="Value of option 2">opt 2</option>
<option>opt 3</option>
</select>

<script type="text/javascript">
function blurt(el) {
if (''SELECT'' == el.nodeName ){
if ( el[el.selectedIndex].value ) {
alert(el[el.selectedIndex].value);
} else if ( el[el.selectedIndex].text ){
alert(el[el.selectedIndex].text);
} else {
alert(''Value and text are empty...'');
}
} else if ( ''INPUT'' == el.nodeName ) {
if ( ''text'' == el.type ){
alert( el.value );
}
// Add cases for radio, checkbox, etc...
}
}
</script>

You may wish to use a switch block to handle the various cases rather
than a bunch of ifs.

(I can then call ".value" or ".innerText" as appropriate)




微软的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


Adam Lipscombe写道:
Adam Lipscombe wrote:
我需要获取表单属性的内容。


没有这样的东西。

在读/写模式下这是字段,


可能你的意思是`form control''。

所以我可以使用getElementById(" name")。value在ReadOnly这只是普通的
^^ ^^^^看到了吗?

文本,所以我可以使用getElementById(" name")。innerText。

我需要动态地执行它,所以无论如何我都可以查看返回的
对象通过getElementById()并确定它是字段还是
标记?


你的措辞表明你的无知。可能你的意思是你想要

来找出返回什么类型的DOM对象,如果键入text,则输入一个HTMLInputElement

对象。或者另一个DOM对象。

(我可以根据需要调用.value或.innerText)
I need to get the contents of a form attribute.
There is no such thing.
In Read/Write mode this is field,
Probably you mean `form control''.
so I can use getElementById("name").value In ReadOnly this is just plain ^^ ^^^^ see?
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?
Your wording shows your cluelessness. Probably you mean that you want
to find out what type of DOM object was returned, an HTMLInputElement
object if type "text" or another DOM object.
(I can then call ".value" or ".innerText" as appropriate)




`。 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


这篇关于从字段或标记获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆