簡體   English   中英

$(this)在javascript中是什么意思

[英]what is the meaning of $(this) in javascript

我有下面的日歷彈出窗口代碼,可以正常工作

<input type="text" value="12/01/2010"  readonly="readonly" name="start_date_1" id="start_date_1" disabled="disabled"/>
  <img src="/images/calendar_date_select/calendar.gif" onclick="new CalendarDateSelect( $(this).previous(), {popup:'force', year_range:10} );" class="calender_image" alt="Calendar"/>

但是我想要的是我的日歷彈出窗口最初將保持禁用狀態,並且在單擊“編輯”按鈕之后,它才應該打開。 我使用disabled="disabled"但由於popup:'force'而無法使用

所以我寫下面的代碼

<script type="text/javascript" 
   function disable_pop_up(){
     if (edit==true)
       new CalendarDateSelect( $(this).previous(), {popup:'force', year_range:10} );
     else
       return false;
   }
</script>


<input type="text" value="12/01/2010"  readonly="readonly" name="start_date_1" id="start_date_1" disabled="disabled"/>
  <img src="/images/calendar_date_select/calendar.gif" onclick="disable_pop_up()" class="calender_image" alt="Calendar"/>

當然,JavaScript會按預期失敗,所以我的問題是我應該在disable_pop_up()編寫什么來完成它?

* 編輯*

我的問題是通過發送$(this)作為參數來解決的

  function disable_pop_up(cal, id){
     disable = document.getElementById(id).disabled
     if (disable==true)
       return false;
     else
       new CalendarDateSelect( cal.previous(), {popup:'force', year_range:10} );
  } 

  onclick="disable_pop_up($(this), 'start_date_1'"

但是我的問題仍然是相同的,為什么我不能在JavaScript函數中編寫類似$("#start_date_1")東西?

$(this)引用調用函數的HTML元素。

$jQuery函數的簡寫。 僅當頁面中已加載了jQuery庫時,此方法才有效。

在第一個示例中, $(this).previous()將引用輸入,因此,如果要更改功能,請嘗試

<script type="text/javascript" >
   function disable_pop_up(){
     if (edit==true)
       new CalendarDateSelect( $("#start_date_1"), {popup:'force', year_range:10} );
     else
       return false;
   }
</script>

ps $函數 ,是主要jQuery函數的簡寫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM