javascript中$(this)的含义是什么

发布于 2024-10-10 00:24:55 字数 1558 浏览 1 评论 0原文

我有以下用于日历弹出窗口的代码,该代码工作正常,

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

但我的问题仍然相同为什么我不能写类似 $( "#start_date_1") 在我的 JavaScript 函数中?

I have following code for Calendar Pop-Up which is working fine

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

but what I want is my calendar pop up will remain disabled initially and after clicking on Edit button only it should get open. I use disabled="disabled" but it's not working due to the popup:'force'

So I write following code

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

Of course JavaScript fails as expected, so my question is what should I write in a disable_pop_up() to accomplish it?

*EDITED *

My problem is get solved by sending $(this) as an argument

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

But still my question remains same why can't I write something like $("#start_date_1") in my JavaScript function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2024-10-17 00:24:55

$(this) 指的是调用函数的 HTML 元素。

$(this) refers to the HTML element of the calling function.

不乱于心 2024-10-17 00:24:55

$jQuery 函数的简写。仅当您的页面中加载了 jquery 库时,这才有效。

$ is a shorthand for the jQuery function. This will only work if you have the jquery library loaded in your page.

在第一个示例中, $(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>

p.s. $ function 是 jQuery 主函数的简写

in your first example, $(this).previous() is going to refer to the input, so if you want to change your function, try

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

p.s. the $ function, is shorthand for the main jQuery function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文