This is a very difficult thing to achieve, apparently. The browser (IE9 in my case) is expecting the value of the onclick
attribute (when set from a script) to be a function reference rather than a string. We can prove this by converting your code into the equivalent JavaScript as shown below.
<script language="javascript">
function yawn()
{
window.alert("hi!");
}
function createNew()
{
b = window.document.createElement( button );
b.value = "button 3";
b.onclick = "yawn()";
window.alert("Button: " + b.outerHTML);
window.document.body.appendChild(b);
}
function enable()
{
window.document.getElementById("action").removeAttribute("disabled");
}
</script>
如果我们这样做,第三纽伦就会出现,但点击将无所作为。 我们需要作出微小的调整,以便在 Java文中完成这项工作。
function createNew()
{
// ...
b.onclick = function() { yawn(); };
// ...
}
Now, if we convert this back to the equivalent perlscript, we can see that it still doesn t work.
sub yawn
{
$window->alert("hi!");
}
sub createNew
{
$b = $window->document->createElement( button );
$b->{value} = "button 3";
$b->{onclick} = sub { $window->yawn(); };
$window->alert("Button: " . $b->{outerHTML});
$window->document->body->appendChild($b);
}
sub enable
{
$window->document->getElementById("action")->removeAttribute("disabled");
}
事实上,情况稍差一点,因为现在,如果你利用你偏爱的超声波来检查 but子3的元素,就没有<条码><<<>>>>。 总而言之,是手。 因此,我们能够做些什么来解决这一问题? 答案实际上很简单——不使用“柏尔”来动态地制造这些元素,而是静态地制造这些元素,并利用“伯尔”来隐藏和展示这些元素。
<html>
<head>
<title>perlscript baby!</title>
</head>
<script language="perlscript">
sub yawn
{
$window->alert("hi!");
}
sub createNew
{
$window->document->getElementById( button3 )->style->{display} = "inline";
}
sub enable
{
$window->document->getElementById("action")->removeAttribute( disabled );
}
</script>
<body>
<input id= enabler type= button value= button 1
onclick= javascript:enable(); />
<input id= action type= button value= button 2 disabled
onclick= javascript:createNew(); />
<input id= button3 type= button value= button 3 style= display:none;
onclick= javascript:yawn(); />
</body>
</html>
This seems to do the job nicely, although I m not sure how well it will fit into your use case. There is of course, one massively weird thing in this code: the onclick
handlers for each of the input
elements explictly states that it is calling a JavaScript function. This is not true, obviously, as those functions are actually PerlScript subroutines. However, if you remove the javascript:
prefix, then the handlers are never called. I think this just further highlights the browser s bias towards JavaScript.
Hope that helps!