InnerHTML删除SELECT中的前导标记 [英] InnerHTML drops leading tag in SELECT

查看:48
本文介绍了InnerHTML删除SELECT中的前导标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XMLRequest,DOM和Ajax相对较新,但我非常喜欢

功能。我的DIV和SPAN更改运行良好,但是我认为我会尝试更改每个控件的实际innerHTML

而不是更改DIV内部的整个控件。


我试过的是:

创建一个< SELECTso ...


< SELECT ID = MyComboBox>

将OPTION> OPT1< / OPTION>

将OPTION> OPT2< / OPTION>

将OPTION> opt3< / OPTION>

< / SELECT>

然后我尝试更改仅仅是MyComboBox的innerHTML。但是当我在
中查看应该填充SELECT的结果字符串

总是缺少第一个< OPTIONtag。换句话说,字符串

应该如下所示:

strFill ="< OPTION> new1< / OPTION>< OPTION> new2< / OPTION> ;< OPTION> new3< /

OPTION>"


....但相反,它看起来像这样:

strFill = QUOT;名new1< / OPTION>< OPTION> NEW2< / OPTION>< OPTION> NEW3< / OPTION>"


口甚至试图迫使主导像这样的开头字符串...

strResult ="< OPTION>" + strFill;

但是当我弹出SELECT

(即MyComboBox.innerHTML = strResult;)时,它仍会剥离领先的OPTION标签。

我检查了结果的innerText属性,它看起来很好。


有什么东西我缺少或有更简单的方法

这个?

无论如何,在我听到某人的声音之前,我猜它会回到DIV /

SPAN的事情。


谢谢你的帮助。

Mark

解决方案

mj ******** @ yahoo.com 写道:


我比较新到XMLRequest,DOM和Ajax但我真的很喜欢

功能。我的DIV和SPAN更改运行良好,但是我认为我会尝试更改每个控件的实际innerHTML

而不是更改DIV内部的整个控件。


我试过的是:

创建一个< SELECTso ...


< SELECT id = MyComboBox>

< OPTION> opt1< / OPTION>

< OPTION> opt2< / OPTION>

< OPTION> opt3< / OPTION>

< / SELECT>

然后我尝试更改仅仅是MyComboBox的innerHTML。但是当我在
中查看应该填充SELECT的结果字符串

总是缺少第一个< OPTIONtag。换句话说,字符串

应该如下所示:

strFill ="< OPTION> new1< / OPTION>< OPTION> new2< / OPTION> ;< OPTION> new3< /

OPTION>"


...但相反,它看起来像这样:

strFill = QUOT;名new1< / OPTION>< OPTION> NEW2< / OPTION>< OPTION> NEW3< / OPTION>"


口甚至试图迫使主导串在这样的开头......

strResult ="< OPTION>" + strFill;

但是当我弹出SELECT

(即MyComboBox.innerHTML = strResult;)时,它仍会剥离领先的OPTION标签。

我检查了结果的innerText属性,它看起来很好。


有什么东西我缺少或有更简单的方法

这个?

无论如何,在我听到某人的声音之前,我猜它会回到DIV /

SPAN的事情。


感谢您的帮助。

Mark



select元素有一个包含所有

选项的选项集合。用它来操纵选项。


-

G?跑Andersson

_____
http://www.guffa.com



select元素有一个包含所有

选项的选项集合。用它来操作选项。



感谢您的快速回复!

我试图将我的javascript保持在最低限度保持我的

页面轻量级。我主要使用Jscript进行事件和Ajax。

我在后端使用ASP.Net(C#脚本)对象来获取我的

数据,然后填充控件使用TextWriter类

将HTML发送回我的页面。

ex:

StringWriter wr = new StringWriter();

HtmlTextWriter writer = new HtmlTextWriter(wr);

我没有尝试过你的方法(但我已经考虑过了)但是我的恐惧

是同样的事情会发生,因为我将填充C#

对象(我的SELECT控件),然后只将HTML发送回

页面。所以,真的没什么区别。我会尝试但是我没有

认为它会有所作为。

还有其他想法吗?


再次感谢,

Mark


< mj ******** @ yahoo.comwrote in message

新闻:11 ********************** @ n76g2000hsh.googlegr oups.com ......


> select元素有一个包含所有
选项的选项集合。用它来操作选项。



感谢您的快速回复!

我试图将我的javascript保持在最低限度保持我的

页面轻量级。我主要使用Jscript进行事件和Ajax。

我在后端使用ASP.Net(C#脚本)对象来获取我的

数据,然后填充控件使用TextWriter类

将HTML发送回我的页面。

ex:

StringWriter wr = new StringWriter();

HtmlTextWriter writer = new HtmlTextWriter(wr);



哇!我认为你真的让生活变得困难!


几乎从来没有必要真正写出HTML标记,

特别是对于webcontrols ...


如果从一个

数据库中获取填充DropDownList所需的数据,你可以简单地绑定它:


MyDDL.DataSource =<从数据库获取数据集> ;;

MyDDL.DataValueField ="<数据集中的列>" ;;

MyDDL.DataTextField ="<数据集中的列>" ;;

MyDDL.DataBind();


如果没有,您可以添加选择像这样的选项:


MyDDL.Items.Add(new ListItem(" OPTION1"," 1"));

MyDDL.Items.Add (new ListItem(OPTION2,2));

MyDDL.Items.Add(new ListItem(" OPTION3"," 3"));


I am relatively new to XMLRequest, DOM and Ajax but I really like the
functionality. I have the DIV and SPAN changes working well but I
thought that I would experiment with changing the actual innerHTML of
each control rather than changing the whole control inside of a DIV.

What I tried was this:
Create a <SELECTso...

<SELECT id=MyComboBox>
<OPTION>opt1</OPTION>
<OPTION>opt2</OPTION>
<OPTION>opt3</OPTION>
</SELECT>

I then tried to change the innerHTML of just "MyComboBox" but when I
view the resulting string that is supposed to populate the SELECT it
is always missing the first <OPTIONtag. In other words, the string
is supposed to look like this:
strFill = "<OPTION>new1</OPTION><OPTION>new2</OPTION><OPTION>new3</
OPTION>"

....but, instead, it looks like this:
strFill = "new1</OPTION><OPTION>new2</OPTION><OPTION>new3</OPTION>"

I even tried forcing a leading string on the beginning like this...
strResult = "<OPTION>" + strFill;
but it still strips off the leading OPTION tag when I pop the SELECT
(ie. MyComboBox.innerHTML = strResult;)
I checked the innerText property of the result and it looks fine.

Is there something that I am missing or is there an easier way to do
this?
Anyway, until I hear from someone, I guess that it is back to the DIV/
SPAN thing.

Thanks for the help.
Mark

解决方案

mj********@yahoo.com wrote:

I am relatively new to XMLRequest, DOM and Ajax but I really like the
functionality. I have the DIV and SPAN changes working well but I
thought that I would experiment with changing the actual innerHTML of
each control rather than changing the whole control inside of a DIV.

What I tried was this:
Create a <SELECTso...

<SELECT id=MyComboBox>
<OPTION>opt1</OPTION>
<OPTION>opt2</OPTION>
<OPTION>opt3</OPTION>
</SELECT>

I then tried to change the innerHTML of just "MyComboBox" but when I
view the resulting string that is supposed to populate the SELECT it
is always missing the first <OPTIONtag. In other words, the string
is supposed to look like this:
strFill = "<OPTION>new1</OPTION><OPTION>new2</OPTION><OPTION>new3</
OPTION>"

...but, instead, it looks like this:
strFill = "new1</OPTION><OPTION>new2</OPTION><OPTION>new3</OPTION>"

I even tried forcing a leading string on the beginning like this...
strResult = "<OPTION>" + strFill;
but it still strips off the leading OPTION tag when I pop the SELECT
(ie. MyComboBox.innerHTML = strResult;)
I checked the innerText property of the result and it looks fine.

Is there something that I am missing or is there an easier way to do
this?
Anyway, until I hear from someone, I guess that it is back to the DIV/
SPAN thing.

Thanks for the help.
Mark

The select element has an options collection that contains all the
options. Use that to manipulate the options instead.

--
G?ran Andersson
_____
http://www.guffa.com


The select element has an options collection that contains all the
options. Use that to manipulate the options instead.

Thanks for the quick reply!
I am trying to keep my javascript to a minimum in order to keep my
pages lightweight. I am mostly using the Jscript for events and Ajax.
I am using ASP.Net (C# scripting) objects on the backend to grab my
data and then populate the controls using the TextWriter classes to
send the HTML back to my pages.
ex:
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);
I have not tried your method (but I have thought about it) but my fear
is that the same thing will happen because I will populate the C#
object (my SELECT control) and then send the just the HTML back to the
page. So, there is really no difference. I will try it but I do not
think that it will make a difference.
Any other thoughts?

Thanks again,
Mark


<mj********@yahoo.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...

>The select element has an options collection that contains all the
options. Use that to manipulate the options instead.

Thanks for the quick reply!
I am trying to keep my javascript to a minimum in order to keep my
pages lightweight. I am mostly using the Jscript for events and Ajax.
I am using ASP.Net (C# scripting) objects on the backend to grab my
data and then populate the controls using the TextWriter classes to
send the HTML back to my pages.
ex:
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);

Wow! I think you''re really making life difficult for yourself!

There''s almost never any need to actually write out the HTML markup,
especially for webcontrols...

If the data required to populate your DropDownList is being fetched from a
database, you can simply bind it:

MyDDL.DataSource = <fetch dataset from database>;
MyDDL.DataValueField = "<column from dataset>";
MyDDL.DataTextField = "<column from dataset>";
MyDDL.DataBind();

If not, you can add the select options like this:

MyDDL.Items.Add(new ListItem("OPTION1", "1"));
MyDDL.Items.Add(new ListItem("OPTION2", "2"));
MyDDL.Items.Add(new ListItem("OPTION3", "3"));


这篇关于InnerHTML删除SELECT中的前导标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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