我特指的是来自ActiveState的IE嵌入式脚本语言"PerlScript“。
我目前有以下内容,但当按下按钮3时,不会发生任何操作。
<html>
<head>
<title>perlscript baby!</title>
</head>
<script language="perlscript" event="onload" for="window">
sub yawn
{
$window->alert("hi!");
}
sub createNew
{
$b = $window->document->createElement('button');
$b->{value} = "button 3";
$b->{onclick} = "yawn()";
$window->alert("Button: " . $b->{outerHTML});
$window->document->body->appendChild($b);
}
sub enable
{
undef $window->document->all('buttn 2')->{disabled};
}
</script>
<body>
<input id='enabler' type='button' value='button 1' onclick='enable()'></input>
<input id='action' type='button' value='button 2' disabled onclick="createNew()"></input>
</body>
</html>发布于 2012-06-09 04:03:55
显然,这是一件很难实现的事情。浏览器(在我的例子中是IE9)期望onclick属性的值(从脚本设置时)是一个函数引用,而不是一个字符串。我们可以通过将您的代码转换为等效的JavaScript来证明这一点,如下所示。
<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>如果我们运行此命令,将出现第三个按钮,但单击它将不会执行任何操作。我们只需要做一个很小的调整就可以在JavaScript中工作。
function createNew()
{
// ...
b.onclick = function() { yawn(); };
// ...
}现在,如果我们将它转换回等价的perlscript,我们可以看到它仍然不能工作。
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");
}实际上,这有点糟糕,因为现在,如果您使用您最喜欢的HTML调试器来检查button 3元素,则根本没有onclick处理程序。那么,我们可以做些什么来解决这个问题呢?实际上,答案非常简单--不要使用PerlScript动态创建元素,而是静态地创建它们,并使用PerlScript隐藏和显示它们。
<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>这似乎很好地完成了这项工作,尽管我不确定它是否适合您的用例。当然,这段代码中有一件非常奇怪的事情:每个input元素的onclick处理程序都显式地声明它正在调用一个JavaScript函数。显然,这不是真的,因为这些函数实际上是PerlScript子例程。但是,如果删除javascript:前缀,则永远不会调用处理程序。我认为这进一步突出了浏览器对JavaScript的偏爱。
希望这能有所帮助!
https://stackoverflow.com/questions/8857925
复制相似问题