2010-09-24 31 views
1

我已經寫了一個jquery,它從存儲在json數組中的數據填充html表。在其中一個字段中,我想爲它創建一個超鏈接。在jQuery腳本中使用href

該表顯示文件列表,其中包含文件名,ID,類型,大小和操作系統的屬性。

我想文件名屬性是我選擇的超鏈接,但我不知道爲什麼它不與下面的代碼工作。如果我刪除(「a」)。attr()並將其作爲默認值,它將顯示文件名coloumn,但如果我在代碼中添加該函數,文件名列將消失。

這裏是下面的代碼:

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
     <script> 
      var files = ${jsonArray} 
      $(document).ready(function() { 
       var table = $('<table border="1"/>').appendTo($('#somediv')); 
       $(files).each(function(i, file) { 
        $('<tr/>').appendTo(table) 
         .append($('<td/>').text(file.FileObject.id)) 
         .append($('<td/>').text($("a").attr("file.FileObject.filename", "http://www.google.com/") )) 
         .append($('<td/>').text(file.FileObject.type)) 
         .append($('<td/>').text(file.FileObject.size)) 
         .append($('<td/>').text(file.FileObject.os)); 
       }); 
      }); 
     </script> 

回答

2
$('<td/>').text($("a").attr("file.FileObject.filename", "http://www.google.com/")) 

應該

$('<td/>').html($("<a>").text(file.FileObject.filename).attr("href", "http://www.google.com/")); 
+0

謝謝朋友上面的代碼工作像一個魅力 – jonney 2010-09-24 08:31:44

+0

不客氣。只是不要忘記接受正確的答案;) – Reigel 2010-09-24 08:44:06

0
.append($('<td/>').text($("a").attr("href", "http://www.google.com/") )) 
0

具有u嘗試使用的file.FileObject.filename代替"file.FileObject.filename"我的意思是除去UR 「」