2011-05-06 4 views
2

C# 개체가 null이 아닌 경우 fadeIn을 실행하려고합니다. 나는 그 세부 사항이 중요하지 않아야한다고 상상할 것이다. 다만 asp.net MVC 3 - 면도기.서버 측 결정을 기반으로 조건부로 jQuery를 실행하는 방법

jQuery를


<script type="text/javascript"> 
function MessageFadeIn(){ 
    $('messageLbl').fadeIn('slow', function() { 
     //Animation complete 
    }); 
} 
</script> 

ASP.NET 나는이 작업을 수행 할 수있는 방법


@if (ViewBag.Message != null) 
{ 
    //run jquery to fade this label in below...the label shouldnt display unless jQuery runs 
    <label id="messageLbl" style="background-color:Red;color:White;">@ViewBag.Message</label> 
} 

?

답변

3

메시지가 null이 아니므로 messageLbl 만 존재합니다.

존재하지 않는 개체에서 페이드 인하면 jQuery에서 오류가 발생하지 않습니다. 그냥 우아하게 실패합니다.

3

HTML에 CSS display: none;style 속성을 추가하십시오. 그러면 label이 기본적으로 표시되지 않습니다.

@if (ViewBag.Message != null) 
{ 
    //run jquery to fade this label in below...the label shouldnt display unless jQuery runs 
    <label id="messageLbl" style="display: none; background-color:Red; color:White;">@ViewBag.Message</label> 
} 

와 jQuery를

(그것이 ID Selector의 표시하는 messageLbl 전에 # 추가) :가 제대로 설명

<script type="text/javascript"> 
$(document).ready(function { 
    $('#messageLbl').fadeIn('slow', function() { 
     //Animation complete 
    }); 
}); 
</script> 

으로 @Raynos는 #messageLbl 경우 문제는 문서에 존재하지 않는 것입니다.

little jsFiddle은 어떻게 작동하는지 보여줍니다.

+1

좋은 정보가 많습니다. - 감사합니다. –

관련 문제