English 中文(简体)
我怎么能用“柏尔文”创建超文纽顿并采取行动?
原标题:How can I create an HTML button with an action and append it to the DOM using PerlScript?

I m专门谈到来自活跃国家的E嵌入文字语言“Perlory”。

I currently have the following, but when button 3 is pressed, no action happens.

<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>
最佳回答

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!

问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签