2012-02-15 19 views
2

當我在javascript中編寫代碼時,我意識到一些奇怪的事情,特別是在使用一些jquery函數時。Reaffected div使jquery變得不可思議

這裏是我的代碼

<?php // Encoded in UTF-8 
    if(isset($_SESSION['username'])) 
    { 
?> 

<script type="text/javascript"> 
    function showEvent(nid) 
    { 
     $j=jQuery.noConflict(); 
     $j.get("getGestionEvent.php?type=show&nid="+nid, function(data){ 
      document.getElementById("eventdiv").innerHTML = data; 
     }); 
    } 
</script> 

<h2> Event </h2> 
<fieldset> 
<legend> Add </legend> 
<div style="margin-top:10px;"> 
    <label for="date"> Date : </label> 
    <span style="margin-left:20px;"> 
     <button id="ButtonCreationDemoButton"> 
      <img src="img/calendar.png" alt="[calendar icon]"/> 
     </button> 
    </span> 
</div> 
<form name="events" action="index.php?p=event" method="post"> 
<div style="margin-top:5px;"> 
    <span style="margin-left:-1px;"> 
     <input type="text" name="ButtonCreationDemoInput" id="ButtonCreationDemoInput"/> 
    </span> 
</div> 
<div style="margin-top:10px;"> 
    <label for="date"> Description : </label> 
</div> 
<div style="margin-top:5px;"> 
    <span style="margin-left:-1px;"> 
     <input type="text" name="desc" id="desc" maxlength="100"> 
    </span> 
</div> 
<div style="margin-top:5px;"> 
    <input type="submit" value="Ajouter"> 
<div> 
</fieldset> 
</form> 
<div id="eventdiv"></div> 
<script type="text/javascript"> 
    // If I don't call the function, the other script doesn't make an error 
    showEvent(0); 
</script> 

<script> 
    $('#ButtonCreationDemoButton').click(
     function(e) { 
     $('#ButtonCreationDemoInput').AnyTime_noPicker().AnyTime_picker().focus(); 
     e.preventDefault(); 
     }); 
</script> 
<?php 
    } 
    else 
    { 
     echo '<p style="color:red"> You cannot see this page. </p>'; 
    } 
?> 

這是一個簡單的形式與2文本框,但我有日曆的圖像的按鈕,使出現在文本框ButtonCreationDemoInput頂部的日曆,讓用戶選擇日期很容易。如果我刪除調用函數showEvent()的那一行,日曆顯示沒有問題。但是,如果我讓該函數在那裏,我得到的錯誤,我不看日曆:

Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function

,並指出就行了:$(「#ButtonCreationDemoButton」)點擊(

順便說一下,showEvent只給予一個叫做eventdiv的div來自數據庫的一個數據表。

這不是我第一次看到這種問題。只有當我重新生成一個div的內容時。

有人可以幫我嗎?

回答

2

$是jQuery對象的標準變量別名。但是你正在使用jQuery.noConflict();。這將刪除$別名。相反,您必須使用jQuery這個詞,即jQuery('#ButtonCreationDemoButton')

基本上,showEvent()被調用,刪除$別名,然後嘗試在以下script標記中使用它。

但是,在您的代碼中,您不只是致電jQuery.noConflict(),而是將其分配給變量$j。這使您可以使用$j作爲jQuery別名,即$j('ButtonCreation...')

+0

非常感謝你! 現在我明白了錯誤,現在工作正常:D – TheBlainer 2012-02-16 12:30:46