在新创建的元素中调用函数的问题 [英] Problem with calling a function within a newly created element

查看:57
本文介绍了在新创建的元素中调用函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想做的是从新创建的

元素中调用函数。但它让我失望了。


这里是引用新创建的元素的行,我现在使用了

的alert函数进行调试。我搞砸了所有这些

报价吗?


//再一次这是关键的代码行,问题区域,无视

var i等...

newTRc1.innerHTML =''\< input type =" text" name = q''+ i +''size =" 60" \> ;;输入:

\< select name = qType''+ i +''onchange =" alert( i);" \> \< option value =" YN"

selected\> Yes / No \< option value =" CK" \> ; Checkbox \< / select \>< br \>'';


谢谢。

Hi,

What I wanted to do is to call a function from a newly created
element. But it stumbled me.

Here''s the line that references the newly created element and I used
the alert function for debugging for now. Did I mess up all these
quotes?

// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHTML =''\<input type="text" name=q''+i+'' size="60"\>;Type:
\<select name=qType''+i+ ''onchange="alert(i);"\>\<option value="YN"
selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'';

Thanks.

推荐答案

5月17日,6:52 * pm,DL< tatata9 ... @ gmail.comwrote:
On May 17, 6:52*pm, DL <tatata9...@gmail.comwrote:




我想要做的是从新创建的

元素中调用函数。 *但它让我失望了。


这里是引用新创建的元素的行,我现在使用了

警报函数进行调试。 *我搞砸了所有这些

报价?


//再一次这是关键的代码行,问题区域,无视

var i等...

newTRc1.innerHTML =''\< input type =" text" name = q''+ i +''size =" 60" \> ;;输入:

\< select name = qType''+ i +''onchange =" alert( i);" \> \< option value =" YN"

selected\> Yes / No \< option value =" CK" \> ; Checkbox \< / select \>< br\>'';


谢谢。
Hi,

What I wanted to do is to call a function from a newly created
element. * But it stumbled me.

Here''s the line that references the newly created element and I used
the alert function for debugging for now. * Did I mess up all these
quotes?

// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHTML =''\<input type="text" name=q''+i+'' size="60"\>;Type:
\<select name=qType''+i+ ''onchange="alert(i);"\>\<option value="YN"
selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'';

Thanks.



好​​的,所以,看起来我好像没想到,

新创建的元素中的事件处理程序还没有由浏览器加载,因此,没有

对这样的事件采取行动。是?如果是这样,我们如何解决这个问题?或者另一种更好的方法来实现相同的目标?


谢谢。

Ok, so, it looks like I wasn''t thinking, the Event handler within the
newly created element hasn''t been loaded by a browser, hence, no
action upon such an event. Yes? And if so, how do we solve this
problem? Or another better approach to achieve the same goal?

Thanks.


DL写道:
DL wrote:

我想要做的是从新创建的

元素中调用函数。但它让我失望了。


这里是引用新创建的元素的行,我现在使用了

的alert函数进行调试。我搞砸了所有这些

报价了吗?
What I wanted to do is to call a function from a newly created
element. But it stumbled me.

Here''s the line that references the newly created element and I used
the alert function for debugging for now. Did I mess up all these
quotes?



是的,你做到了。

Yes, you did.


//再次这是关键的代码行,问题区域,无视

var i等...

newTRc1.innerHTML =''\< input type =" text" name = q''+ i +''size =" 60" \> ;;输入:
// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHTML =''\<input type="text" name=q''+i+'' size="60"\>;Type:



^ start end ^ ^ start

^start end^ ^start


\< select name = qType''+ i +''onchange =" alert(i);" \> \< option value =" YN"
\<select name=qType''+i+ ''onchange="alert(i);"\>\<option value="YN"



end ^ ^ start [1] ^^^ [2]

end^ ^start[1] ^^^[2]


selected\>是/否\<选项值=" CK" \>复选框\< / select \>< br \>'';
selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'';



^ end


那里。因为您没有正确划分所有属性值

[^ 1],您将生成


< select name = qType42onchange =" alert(i );">


如果i == 42.您希望用户代理在这里做什么?


在划分时所有属性值都带有 -


< select name =" qType42" onchange =" alert(i);">


- 或包括遗漏的空格 -


< select name = qType42 onchange =" alert(i);">


- 或者他们两个都是快速修复,我强烈建议你停止烹饪专有(innerHTML)标签汤并开始使用DOM Level 2创建者

和mutator方法而不是。使用一些用户定义的包装器,它们可以比现在更高效地使用它们。

http://www.w3.org/TR/DOM-Level-2-Core/


此外,如果你生成'i'的标识符而不是'i''[^ 2]的当前

值,它将被解析为执行事件处理程序

属性值,这可能不是你想要的。


BTW,`<''和`>''不需要在字符串文字中转义; `< /''(ETAGO)
如果源代码在HTML文档中,
应该以`< \ /''的形式进行转义。

PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent.indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac'')!= -1

)// Plone,register_function.js:16

^end

There. Because you have not properly delimited all your attribute values
[^1], you will generate

<select name=qType42onchange="alert(i);">

if i == 42. What do you expect of the user agent to do here?

While delimiting all attribute values with " --

<select name="qType42"onchange="alert(i);">

-- or including the missing whitespace --

<select name=qType42 onchange="alert(i);">

-- or do both of them would be a quick fix, I strongly suggest you stop
cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator
and mutator methods instead. With a few user-defined wrappers they can be
even more efficient to use than what you have now.

http://www.w3.org/TR/DOM-Level-2-Core/

Furthermore, if you generate the identifier of `i'' instead of the current
value of `i'' [^2], it will be resolved on execution of the event handler
attribute value, which is probably not what you want here.

BTW, `<'' and `>'' do not need to be escaped in string literals; `</'' (ETAGO)
should be escaped as `<\/'' if the source code is within an HTML document.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16

< br>

DL写道:
DL wrote:

5月17日下午6:52,DL< tatata9 ... @ gmail.comwrote:
On May 17, 6:52 pm, DL <tatata9...@gmail.comwrote:

> newTRc1.innerHTML =''\< input type =" text" name = q''+ i +''size =" 60" \> ;;输入:
\< select name = qType''+ i +''onchange =" alert(i);" ; \> \<选项值=" YN"
已选择\>是/否\<选项值=" CK" \>复选框\< / select \>< br\> '';
>newTRc1.innerHTML =''\<input type="text" name=q''+i+'' size="60"\>;Type:
\<select name=qType''+i+ ''onchange="alert(i);"\>\<option value="YN"
selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'';



好​​的,所以,看起来我好像没想到,

新创建的元素中的事件处理程序还没有由浏览器加载,因此,没有

对这样的事件采取行动。是?


Ok, so, it looks like I wasn''t thinking, the Event handler within the
newly created element hasn''t been loaded by a browser, hence, no
action upon such an event. Yes?



不完全。事件处理程序之前存在,作为本机代码。事件

处理程序属性值是一个字符串,传递给

脚本引擎,该引擎使用它来生成一个函数(主要事件

listener)由事件处理程序在事件上调用。


然而,由于生成的代码因缺少onchange属性而缺少

将相关HTML代码与其余部分分开,没有什么可以传递给脚本引擎,并且没有创建主要事件监听器

可能是呼吁事件。

Not quite. The event handler exists before, as native code. The event
handler attribute value is a string of characters that is passed on to the
script engine which uses it to generate a function (the primary event
listener) that is called by the event handler on event.

However, as the generated code lacks the `onchange'' attribute due to missing
separation of the relevant HTML code from the rest here, there is nothing to
pass on to the script engine, and no primary event listener is being created
that could be called on event.


或者另一种更好的方法来实现相同的目标?
Or another better approach to achieve the same goal?



请参阅我的其他跟进。

PointedEars

-

任何人

a标签打了''这个页面最好用浏览器X查看''这个网页似乎渴望过去的糟糕时光,在网络之前,

当你几乎没有机会阅读另一台计算机,另一台文字处理器或其他网络上的文件。 - Tim Berners-Lee

See my other followup.
PointedEars
--
Anyone who slaps a ''this page is best viewed with Browser X'' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


这篇关于在新创建的元素中调用函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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