2012-02-09 2 views
0
function removeNewsItem(id,idStamp,obj){ 
$('#whiteCover')show(); 

$('#contentOfBox').html= ('Are you sure you want ot completely remove this news item?<br/><br/><div style="text-align:center;"><input type="button" class="button blue medium" onclick="removeNewsItemConfirmed(\'' + id + '\',\''+idStamp+'\','+**obj**+')" value="Yes"/> <input type="button" class="button red medium" value="No" onclick="resetContentBox();"/></div>'); 

$("#whiteCoverContainer").fadeIn(); 
} 

function removeNewsItemConfirmed(id,idStamp,obj){ 
    alert(obj); 
    $(obj).remove(); 
    return; 
} 

<tr><td onclick=removeNewsItem('111','134234',this)></td></tr> 

removeNewsItem을 사용하여 td를 클릭하면 'this'요소를 다음 함수로 전달할 수없는 것처럼 보입니다. 사용자가 클릭하면 삭제를 확인하는 메시지가 표시됩니다 그들이 그것을 클릭하면 'this'는 properl, 또는 더 많은 것을 fleure 할 수없는 point에 건네지 못하고 있습니다 ...javascript에서 'this'를 한 함수에서 다른 함수로 전달 하시겠습니까?

누군가 도와주세요 : 어떻게 다른 인라인 onclick 이벤트에 obj를 전달할 수 있습니까? 위처럼?

+0

. JavaScript로 프로그래밍하는 방법을 실제로 알고 있습니까? –

+0

당신은 따옴표 (")를 잊어 버렸지 만 이것은 HTML 문자열이지만 dom 객체를 나타내지는 않습니다. 그러나"this "는 올바르게 전달되어야합니다 : http://stackoverflow.com/questions/925734/whats-this-in-javascript -onclick – Julien

+0

그래, 그 코드는 뭐니?'$ ('# contentOfBox') .html ='???. jquery와'onclick' ??? 나는 올바른 js를 먼저 배워야한다고 생각한다. , 그리고 나서 적절한 코드를 가지고 다시 여기로 오십시오. – elclanrs

답변

0

은 클릭 된 요소를 기준으로합니다. 자체 onclick 핸들러가있는 새 요소를 추가하면 this이 참조됩니다. 그것을 만든 사람이 아닙니다.

그래서 이것은 당신이

onclick="removeNewsItemConfirmed(\'' + id + '\',\''+idStamp+'\','+**obj**+')" 

이 대신 당신이 추적 할 수있는 기준을 만들 필요가 생각하는 방식으로 작동하지 않습니다.

function removeNewsItem(id,idStamp,obj){ 
    $(obj).addClass('markedForDeletion'); 
    $('#whiteCover')show(); 
    $('#contentOfBox').html= ('Are you sure you want ot completely remove this news item?<br/><br/><div style="text-align:center;"><input type="button" class="button blue medium" onclick="removeNewsItemConfirmed()" value="Yes"/> <input type="button" class="button red medium" value="No" onclick="resetContentBox();"/></div>'); 

    $("#whiteCoverContainer").fadeIn(); 
} 

function removeNewsItemConfirmed(){ 
    $('.markedForDeletion').remove(); 
} 

전반적으로이 모든 것을 처리하는 훨씬 더 좋은 방법이 있지만 잘하면이 사실을 알게됩니다. (기존 마크 업을 너무 많이 변경하지 않고)과 같을 수

+0

이것도 마찬가지입니다! jQuery를 처음 접해 보니 꽤 깔끔 해 보입니다. – John

1

이벤트를 사용해야하는 것 같습니다. jQuery를 사용하는 경우 on()을 사용하여 속성 대신 이벤트를 첨부하십시오. More about on().

onclick 대신이 함수를 사용하면 this이 함수에 반환됩니다. 예를 들어 :

<table> 
<tr><td>123</td></tr> 
<tr><td>456</td></tr> 
</table> 

그리고 일부 JS

// Will have access to this and has ev which is the event 
function removeNewsItem(ev){ 
    var $this = $(this); 
} 

$('table').on('click', 'td', removeNewsItem); 

당신이 당신의 테이블 셀에 데이터 값을 연결해야하는 경우 당신이 할 수있는 데이터의 속성과 jQuery's data()와 그.

+0

그리고 이것은 여전히 ​​단단한 것처럼 보입니다! 나는 모든 것을 이해하기 위해 약간의 일을해야 할 것입니다. 많이 고마워요! 당신 모두에게 대답합니다. :) – John

1

귀하의 이벤트 :

$(".newsItem").click(function(){ 
    var id = $(this).attr('id'); 
    var idStamp = $(this).attr('idStamp'); 
    $('#whiteCover')show(); 

    $('#contentOfBox').html= ('Are you sure you want ot completely remove this news item?<br/><br/><div style="text-align:center;"><input type="button" class="button blue medium" onclick="removeNewsItemConfirmed(\'' + id + '\',\''+idStamp+'\')" value="Yes"/> <input type="button" class="button red medium" value="No" onclick="resetContentBox();"/></div>'); 

    $("#whiteCoverContainer").fadeIn(); 
}); 

function removeNewsItemConfirmed(id,idStamp){    
    alert(id);    
    $("#"+id).remove();    
    return;    
}  

다음과 같이 표시됩니다 귀하의 HTML : jQuery를 방법의 적절한 사용이 아니다

<tr><td id='111' idStamp='134234' class='newsItem'></td></tr> 
+0

대단히 고마워요! 에 추가 속성을 추가 할 생각은 전혀 없었습니다. – John

관련 문제