2013-11-27 12 views
0

我是新來的JavaScript和jQuery我使用「追加」功能中的div追加的一個標籤,其工作正常傳遞變量附加標籤內jQuery中

$('#div1').append('<a href=./1.php>'+test+'</a>'); 

var test= "<script>"; 
test+="alert("hello1")"; 
test+="<"; 
test+="/script>"; 

這個代碼顯示hello1

我的問題是,我想與測試通過可變這樣

$('#div1').append('<a href=./1.php>'+test(variable)+'</a>'); 

var test= "<script>"; 
test+="alert(variable)"; 
test+="<"; 
test+="/script>"; 

有反正還是我做錯了?

在此先感謝

+0

爲什麼錨標記內添加JavaScript?爲什麼不直接將它添加到'body'(或者甚至更好地放在'head')。當用戶點擊錨點時,你不想調用一個功能嗎? –

+0

你爲什麼要這麼做?這只是在你添加標籤時馬上提醒。 – putvande

回答

0

您需要使用javascript functions返回一個字符串;

function test(variable) { 
    return "<script> alert('" + variable + "'); </" + "script>"; 
} 
0
你可能想要做用戶點擊的東西

$('#div1').append('<a href="1.php">link text</a>').click(function() { 
    alert("hello1"); 
});