2014-12-02 75 views
4

我試圖啓動一個實例數據表用下面的代碼替換...

<script type="text/javascript" language="javascript" class="init"> 
$(document).ready(function() { 
     $('#example').DataTable(); 
}); 
</script> 

但是當我通過網絡的訪問HTML瀏覽器顯示的「未捕獲的SyntaxError:意外的數字」錯誤,這是源從瀏覽器的外觀...

<script type="text/javascript" language="javascript" class="init"> 
0 11 4 3 2 1 0document).ready(function() { 
    0 11 4 3 2 1 0'#example').DataTable(); 
}); 
</script> 

正如你可以看到一些指令已經被替換爲數字「0 11 4 3 2 1「,我不知道是什麼原因造成的。

jQuery是來自Google的src和來自其CDN的DataTables JavaScript。

我從使用CGI的Perl腳本創建HTML,打印Content-type:text/html標題並使用不同的!DOCTYPE ...但仍然沒有任何內容。編輯代碼顯示沒有隱藏的字符。

您的幫助將不勝感激。

最好, e。

編輯:這是Perl代碼創建HTML ...

use Switch; 
use CGI qw/:standard/; 
use CGI::Carp 'fatalsToBrowser'; 

print "Content-type:text/html\r\n\r\n"; 

print qq{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0"> 
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script language="JavaScript" type="text/javascript" src="js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" language="javascript"> 
/* <![CDATA[ */ 
$(document).ready(function() { 
     $('#example').DataTable(); 
}); 
/* ]]> */ 
</script> 
</head>}; 
+0

class init是做什麼的? – 2014-12-02 22:42:09

+0

請顯示生成HTML的Perl代碼。 – ThisSuitIsBlackNot 2014-12-02 22:44:41

+0

@ Bhojendra-C-LinkNepal不確定,是所有嘗試之一,但是在原始DataTables示例文件之一。 – Emiliano 2014-12-02 22:57:18

回答

4

的問題是,qq插值變量和你的HTML字符串中包含的特殊變量$(

$(document).ready(function() { 
     $('#example').DataTable(); 

根據perldoc perlvar

$(

The real gid of this process. If you are on a machine that supports membership in multiple groups simultaneously, gives a space separated list of groups you are in. The first number is the one returned by getgid() , and the subsequent ones by getgroups() , one of which may be the same as the first number.

您的字符串中的每個匹配項$(都將替換爲您的Web服務器用戶所屬的GID列表。

你可以看到這個命令行:

perl -wE 'say qq{$(document).ready(function()}' 

輸出我的系統上

3000 3000document).ready(function() 

使用q而不是qq來避免將事物插入爲Perl變量。

+2

或者,如果你確實想要插入一些變量,請轉義每個不能啓動變量的'$'(例如'\ $(document).ready') – ysth 2014-12-02 23:24:00

+0

另一個我用過的替代方法是,改變你的jQuery onReady,你可能無論如何都應該使用'jQuery(function(jQ){...'),然後在你使用'$'的地方使用'jQ'。 – Ashley 2014-12-03 03:31:48

+0

@Ashley當你開始混淆你的JavaScript代碼時,你可能應該看看把它放在單獨的靜態文件中,或者使用[模板工具包](http://www.template-toolkit.org/)這樣的模板系統。 – ThisSuitIsBlackNot 2014-12-03 15:45:11

相關問題