2009-08-18 7 views
0

아무도 내가 알고있는 자바에서 html로 전달 변수를 대체 할 수 있습니까?javascript로 함수 변수를 대체 할 수있는 방법은 없나요?

예 :

나는 아래와 같은 코드가있는 경우 : 나는 자바 스크립트와 'CDE'에 변수 'ABC'를 대체 할 수

<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> 

어떤 방법을?

+1

왜 함수에 cde를 전달하지 않습니까? – rahul

답변

1

당신 (다른 사람에 의해 언급 된 바와 같이), 그러나 내가 의심 할 수 있습니다 대신 지금 부르고 무엇 addNewRowToTable('cde')를 호출 onclick 속성을 바꾸려면

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를 변경하면 코드에 영향을 미칩니다.

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

, 나는 리 바인드 이벤트를 제안 우리가 실제로 무엇을하려고했는지 알면 더 유용한 대답을 얻을 수 있습니다. 문제를 다룰 수있는 더 좋은 방법이 있습니다. 그러나 그것이 무엇인지를 제시 할 수있는 문맥이 필요합니다.

관련 문제