2009-08-18 62 views
0

有誰知道我該如何用Javascript替換html中的傳遞變量?任何我可以用javascript代替函數變量的方式?

實施例:

如果我有如下代碼:

<table width="150" border="0" cellspacing="0" cellpadding="2" id="productBox"> 
<tr> 
    <td valign="top" height="19" id="td4"><img onclick="addNewRowToTable('abc')" src="images/add0.gif" onmouseover="this.src=\'images/add1.gif\'" onmouseout="this.src=\'images/add0.gif\'" class="handCursor" width="49" height="19"></td> 
</tr> 
</table> 

任何方式,我可以用JavaScript替換變量 'ABC' 到 'CDE'?

+1

你爲什麼不把cde傳遞給函數? – rahul 2009-08-18 05:51:02

回答

1

可以(如由他人注意的),但我懷疑如果我們知道你實際上想要做什麼,你可能會得到更有用的答案;當然有更好的方法來處理你的問題,但我需要上下文來提示它可能是什麼。

0

您可以傳遞一個全局變量來運行。在代碼中定義這個變量更高。

<script> 
var globalVar='cdb'; 
</script> 

<table width="150" border="0" cellspacing="0" cellpadding="2" id="productBox"> 
<tr> 
    <td valign="top" height="19" id="td4"><img onclick="addNewRowToTable(globalVar)" src="images/add0.gif" onmouseover="this.src=\'images/add1.gif\'" onmouseout="this.src=\'images/add0.gif\'" class="handCursor" width="49" height="19"></td> 
</tr> 
</table> 

如果你以後會更改globalVar,它會影響你的代碼。

0

如果你想更換onclick屬性調用addNewRowToTable('cde')什麼,而不是它現在打電話,我會建議重新綁定事件:

var img = // Here you would get the <img> somehow 
img.onclick = function() { addNewRowToTable('cde'); }; 
相關問題