用perl创建html [英] creating html with perl

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

问题描述

 <报头GT; 
< script type =text / javascript>
function hideit()
{

var x1 = document.getElementsByTagName(ol)。item(0);
var x = document.getElementsByTagName(ol);
for(var i = 0; i< x.length; i ++)
x [i] .style.display =none;

function showit()
{
var x1 = document.getElementsByTagName(ol)。item(0);

var x = document.getElementsByTagName(ol);
for(var i = 0; i< x.length; i ++)
x [i] .style.display =;
}
< / script>< header>
< body>
foreach $ key(keys%{$ stats_data {$ out}}){

print $ indexfd(< input onclick = \showit()\type = \\ \\button \id = \<?perl echo $ key;?> \value = \showit\>< br>< input onclick = \hideit( )\type = \button \id = \<?perl echo $ key;?> \value = \hideit\>< br>< b> ;< ol id = $ key> $ stats_data {$ out} {$ key}< ol id = $ key>< / b>< br>);
}< / body>

我正在用perl创建一个html文档。我为每个perl变量编写了一个事件mouseover和mouseout(通过循环)。但看起来事件同时控制着所有的变量。我如何只写一次事件,但使它可以单独应用于每个项目:这是我目前的



但是这个html显示时,不让我分别控制每个 $ key 的事件。



即使每个$ key都创建了按钮,一方面控制所有的 $ stats_data {$ out} {$ key} 。我甚至尝试过

解决方案

您的代码看起来像是在尝试将Perl与HTML混合使用在某种程度上你会使用PHP。 这在Perl中不起作用。



我试着先修复你的Perl代码。这将打印到您的文件句柄(我省略),但不会给你工作的JavaScript代码。我没有改变,因为我不明白你想做什么。以后再详细介绍。

  use strict; 
使用警告;
#开放的$ indexfd文件句柄在这里..

#HTML页面的打印头
打印$ indexfd<<< HTML
< html>
< header>
< script type =text / javascript>
function hideit(){
var x1 = document.getElementsByTagName(ol)。item(0);
var x = document.getElementsByTagName(ol);
for(var i = 0; i< x.length; i ++)
x [i] .style.display =none;
}
function showit(){
var x1 = document.getElementsByTagName(ol)。item(0);
var x = document.getElementsByTagName(ol);
for(var i = 0; i< x.length; i ++)
x [i] .style.display =;
}
< / script>
< / header>
< body>
HTML
;

#如果我正确地看到这一点,%stats_data看起来像这样:
my%stats_data =(
'out1'=> {
'key1'=> ;'val1',
'key2'=>'val2',
},
'out2'=> {
'key1'=>'val1',
'key2'=>'val2',
},
);
my $ out ='out1'; #你需要从某处$
#打印按钮的每个数据thingy - 你需要排序它们可能
foreach我的$ key(排序键%{$ stats_data {$ out}}){
print $ indexfd $ b $ q qq〜< input onclick =showit()type =buttonid =$ keyvalue =showit>< br />〜,
qq〜< input onclick =hideit()type =buttonid =$ keyvalue =hideit>< br />〜,
#因为$ stats_data { $ out}是一个散列引用,你需要 - >这里的运算符
qq〜< ol id =$ key>< li>< b> $ stats_data {$ out} - > {$ key}< / b>< / li> < /醇><峰; br />〜;
}

print $ indexfd qq〜< p>更多html ...< / p>< / body>< / html>〜;

所以有几件事值得一提。

  print $ indexfd(< input onclick = \showit()\type = \button \id = \<?perl echo $ key;?> \value = \showit\>< br>< input onclick = \hideit()\type = \button \id = \\ \\<?perl echo $ key;?> \value = \hideit\>< br>< b>< ol id = $ key> $ stats_data {$ out} { $ key}< ol id = $ key>< / b>< br>); 

在这段相当长的代码中,您使用<?perl echo $键; ?> 它看起来像php并且不起作用。您还使用了< ol id = $ key> ,因为您使用了双引号... 。 Perl将-delimited字符串中的变量替换为它们的值,不需要类似php的东西。为了节省您在HTML代码中转义所有双引号的努力,您可以使用 qq -Operator。参见 perlop a> for more infos。



我在我的评论中解释了%stats_data 数据结构。



为了打印大量的HTML,我使用了 HERE文档 a>。



让我们来谈谈您的JavaScript。


即使按钮确实为每个 $ key 创建,点击其中一个,控制 $ stats_data {$ out} {$ key} 所有。

这是因为您设计 hideit() showit()函数在你想达到什么。如果你看看我的%stats_data ,你会发现'out1'中有几个键。这可以让foreach循环为每个键打印一组按钮。这两个按钮都与它们的ID具有相同的密钥,就像ol一样。这是不正确的。 id-属性必须是唯一的



此外,您的html中还有一些基本问题,我也可以自由修复。如果您打开< ol id =foo> 容器,则需要将其关闭,如< / ol> 。由于< ol> 是一个块级元素,因此您不应将内联元素< b> 在它之外,而是在ol的< li> 元素内(我省略了)。最好是将css``style =font-weight:bold分配给li项目,或者更好地给它们类。



最后一次猜测你想用JavaScript做什么。如果你有几段文字,你想用按钮隐藏它们,你可以像我这里未经测试的代码那样做。



Javascript:

  function toggle(id){
if(document.getElementById(id).style.display =='block'){
document.getElementById(id).style.display ='none';
} else {
document.getElementById(id).style.display ='block';


$ / code $ / pre
$ b

HTML:

 < div> 
< input type =buttonname =toggle1id =toggle1onclick =toggle('p1')/>
< p id =p1> Lorem ipsum dolor set ... foo很多文字1.< / p>
< input type =buttonname =toggle2id =toggle2onclick =toggle('p2')/>
< p id =p2> Lorem ipsum dolor set ... foo很多文字2.< / p>
< / div>

在这种情况下,函数会检查段落是否显示。显示值需要设置为'none'或'block',因为p元素是块级元素。



请尝试发布更具体的内容编辑:
在下面的代码中,我更改了以下代码:

JS函数都将一个id(键)作为参数。他们只显示或隐藏与此密钥关联的列表。我改变了按钮的ID,所以它们不一样。我还在每一对按钮和列表中添加了一个div,以使它更清晰地表明属于哪里。

  use strict; 
使用警告;
#打开$ indexfd文件句柄到这里..
my $ indexfd;

#HTML页面的打印头
打印$ indexfd<< HTML< b $ b< html>
< header>
< script type =text / javascript>
function hideit(key){
document.getElementById(key).style.display =none;
}
函数showit(key){
document.getElementById(key).style.display =;
}
< / script>
< / header>
< body>
HTML
;

#如果我正确地看到这一点,%stats_data看起来像这样:
my%stats_data =(
'out1'=> {
'key1'=> ;'val1',
'key2'=>'val2',
},
'out2'=> {
'key1'=>'val1',
'key2'=>'val2',
},
);
my $ out ='out1'; #你需要从某处$

foreach我的$ key(排序键%{$ stats_data {$ out}}){
print $ indexfd
qq〜< div id =div_ $ key> \\\
〜,#在$ key-elements上添加div
qq〜< input onclick =showit('$ key')type =buttonid =btn_show_ $ keyvalue =showit> \\\
〜,
qq〜< input onclick =hideit('$ key')type =buttonid =btn_show_ $ key value =hideit>< br /> \\\
〜,
qq〜< ol id =$ key>< li>< b> $ stats_data {$ out} - > {$ key}< / b>< / li>< / ol> \\\
〜,
qq〜< / div> \\\
〜;
}

print $ indexfd qq〜< p>更多html ...< / p>< / body>< / html>〜;


<header>
<script type="text/javascript">
function hideit()
{

var x1 = document.getElementsByTagName("ol").item(0);
var x = document.getElementsByTagName("ol");
for (var i = 0; i < x.length; i++)
  x[i].style.display="none";
}
function showit()
{
var x1 = document.getElementsByTagName("ol").item(0);

var x=document.getElementsByTagName("ol");
for (var i = 0; i < x.length; i++)
 x[i].style.display="";
}
</script><header>
<body>
foreach $key (keys %{$stats_data{$out}})  {

    print $indexfd ("<input onclick=\"showit()\" type=\"button\" id=\"<?perl echo     $key; ?>\" value=\"showit\"><br><input onclick=\"hideit()\" type=\"button\" id=\"<?perl   echo $key; ?>\" value=\"hideit\"><br><b><ol id=$key>$stats_data{$out}{$key}<ol id=$key>  </b><br>");
        }</body>

I am creating an html document with perl. I wrote an event mouseover and mouseout to happen for every perl variable (over a loop). But looks like the event controls all the variables at the same time. How do I write the event only once but enable it to individually be applied for each item: this is what I have currently

but this html when displayed, does not let me control the event separately for each $key.

Even though the buttons do get created for each $key, clicking on one, controls the $stats_data{$out}{$key} of all.

I even tried passing the id to the show/hide script, but no luck.

解决方案

Your code looks like you are trying to mix Perl with HTML in a way you would use PHP. This does not work in Perl.

I tried fixing your Perl code first. This will print to your filehandle (which I omitted), but will not not give you the working JavaScript code. I did not change that since I did not understand what you want to do. More on that later.

use strict;
use warnings;
# open of $indexfd filehandle goes here..

# print head of HTML page
print $indexfd <<HTML
<html>
  <header>
    <script type="text/javascript">
    function hideit() {
      var x1 = document.getElementsByTagName("ol").item(0);
      var x = document.getElementsByTagName("ol");
      for (var i = 0; i < x.length; i++)
        x[i].style.display="none";
    }
    function showit() {
      var x1 = document.getElementsByTagName("ol").item(0);
      var x=document.getElementsByTagName("ol");
      for (var i = 0; i < x.length; i++)
       x[i].style.display="";
    }
    </script>
  </header>
  <body>
HTML
;

# If I see this correctly, %stats_data looks like this:
my %stats_data = (
  'out1' => {
    'key1' => 'val1',
    'key2' => 'val2',
  },
  'out2' => {
    'key1' => 'val1',
    'key2' => 'val2',
  },
);
my $out = 'out1'; # you need the $out from somewhere
# print buttons for each data thingy - you'll want to sort them probably
foreach my $key (sort keys %{$stats_data{$out}})  {
  print $indexfd 
    qq~<input onclick="showit()" type="button" id="$key" value="showit"><br />~,
    qq~<input onclick="hideit()" type="button" id="$key" value="hideit"><br/>~,
    # Because $stats_data{$out} is a hash ref you need the -> operator here
    qq~<ol id="$key"><li><b>$stats_data{$out}->{$key}</b></li></ol><br/>~;
}

print $indexfd qq~<p>more html...</p></body></html>~;

So there are a few things worth mentioning.

    print $indexfd ("<input onclick=\"showit()\" type=\"button\" id=\"<?perl echo     $key; ?>\" value=\"showit\"><br><input onclick=\"hideit()\" type=\"button\" id=\"<?perl   echo $key; ?>\" value=\"hideit\"><br><b><ol id=$key>$stats_data{$out}{$key}<ol id=$key>  </b><br>");

In this rather long line of code you used <?perl echo $key; ?> which looks like php and doesn't work. You also used <ol id=$key> which works because you used double-quotes "...". Perl substitutes the variables inside the "-delimited string for their values. You don't need that php-like stuff. In order to save yourself the effort of escaping all the double quotes in the HTML code you can use the qq-Operator. See perlop for more infos.

I explained about the %stats_data data structure in my comments.

For printing the large chunk of HTML, I used HERE docs.

Let's talk about your JavaScript now.

Even though the buttons do get created for each $key, clicking on one, controls the $stats_data{$out}{$key} of all.

This is because of the way you designed your hideit() and showit() functions. I'm not really ceratin what you want to achieve. If you look at my %stats_data you'll see that there are several keys in 'out1'. That lets the foreach-loop print a set of buttons for each of those keys. The buttons both have the same key as their id, as does the ol. That is not correct. An id-attribute has to be unique.

Furthermore, there were some basic issues in your html which I took the liberty to fix as well. If you open an <ol id="foo"> container, you need to close it like </ol>. Since <ol> is a block-level element, you shouldn't put the inline element <b> outside it, but rather inside the ol's <li> elements (which I omitted). It would be better yet to just assign css ``style="font-weight: bold" to the li items or better yet give them classes.

I'll take one last guess at what you were trying to do with the JavaScript. If you have several paragraphs of text and you want to hide them with buttons, you could do that like my untested code here.

Javascript:

function toggle(id) {
  if (document.getElementById(id).style.display == 'block' ) {
    document.getElementById(id).style.display = 'none';
  } else {
    document.getElementById(id).style.display = 'block';
  }
}

HTML:

<div>
  <input type="button" name="toggle1" id="toggle1" onclick="toggle('p1')" />
  <p id="p1">Lorem ipsum dolor set ... foo a lot of text 1.</p>
  <input type="button" name="toggle2" id="toggle2" onclick="toggle('p2')" />
  <p id="p2">Lorem ipsum dolor set ... foo a lot of text 2.</p>
</div>

In this case, the function checks whether the paragraph is shown or not. The display-value needs to be set to 'none' or 'block', because the p-element is a block-level element.

Please try to post more specific information about the data you use with your script if you need any more help.

EDIT: In the following code I changed the JS functions to both take an id (the key) as a parameter. They only show or hide the lists associated with this key. I changed the button's ids so they're not the same. I also added a div around each pair of buttons and list to make it clearer what belongs where.

use strict;
use warnings;
# open of $indexfd filehandle goes here..
my $indexfd;

# print head of HTML page
print $indexfd <<HTML
<html>
  <header>
    <script type="text/javascript">
    function hideit(key) {
      document.getElementById(key).style.display = "none";
    }
    function showit(key) {
      document.getElementById(key).style.display = "";
    }
    </script>
  </header>
  <body>
HTML
;

# If I see this correctly, %stats_data looks like this:
my %stats_data = (
  'out1' => {
    'key1' => 'val1',
    'key2' => 'val2',
  },
  'out2' => {
    'key1' => 'val1',
    'key2' => 'val2',
  },
);
my $out = 'out1'; # you need the $out from somewhere

foreach my $key (sort keys %{$stats_data{$out}})  {
  print $indexfd 
    qq~<div id="div_$key">\n~, # Add a div around the $key-elements
    qq~<input onclick="showit('$key')" type="button" id="btn_show_$key" value="showit">\n~,
    qq~<input onclick="hideit('$key')" type="button" id="btn_show_$key" value="hideit"><br/>\n~,
    qq~<ol id="$key"><li><b>$stats_data{$out}->{$key}</b></li></ol>\n~,
    qq~</div>\n~;
}

print $indexfd qq~<p>more html...</p></body></html>~;

这篇关于用perl创建html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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