找回密码
 注册

QQ登录

只需一步,快速开始

查看: 301|回复: 0

Understand the object of $(this) in jquery

[复制链接]
发表于 2013-10-27 03:06:55 | 显示全部楼层 |阅读模式
Object which serves as the parameter to the function calls are constructed before you make the call.  So you must have an appropriate "this" before you use it. see the example:
  1. $(".edit").editInPlace({
  2.         url: "./server.php",
  3. params: "folder=" + $(this).attr('folder')

  4.         //show_buttons: true
  5. //$('.edit').attr('folder')
  6.     });
复制代码
with the html is as follows:
  1. <span class="edit" folder="folderName">text to edit</span>
复制代码

Won't fire the code as expected. data "folderName" will not pass to the parameter: you don't have an appropriate "this".
.attr("folder") will work fine, as you could test by, say,
  1.     $(".edit").click(function(evt) {
  2.         alert($(this).attr("folder"));
  3.     });
复制代码

Something like this might help restore "this" context:
  1. $(".edit").each(function() {
  2.     // now "this" is your HTML element
  3.     $(this).editInPlace({
  4.         url: "./server.php",
  5.         params: "folder=" + $(this).attr('folder')
  6.         // ...
  7.     });
  8. });
复制代码




您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-5-27 23:06 , Processed in 0.013978 second(s), 17 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表