admin管理员组文章数量:1278882
Up until this point, I haven't done much in javascript. I recently started playing with jQuery and came across a tutorial on how to do Edit in Place on a form.
The resulting code works perfectly if you hard-code the id of your container div in the function, but I'm having issues generalizing it for an id passed in to the function.
The code starts like this:
$(document).ready(function() {
setClickable($('#editInPlace'));
});
function setClickable(target)
{
$(target).click
(
function()
{
//Do some dom manipulation
$('.buttonA').click(function() { saveChanges(this, false); });
$('.buttonB').click(function() { saveChanges(this, true); });
}
)
.mouseover(function() { $(this).addClass("editable"); })
.mouseout(function() { $(this).removeClass("editable"); });
}; //end of function setClickable
function saveChanges(obj, cancel)
{
//Handle the save changes code
//Reset the div
$(obj).parent().after('<div id="editInPlace">' + t + '</div>').remove();
//Call setClickable
setClickable($('#editInPlace'));
}
The part I'm having issues with is the last call to setClickable
inside of the saveChanges
function. If I hard-code the value of $('#editInPlace')
, it works fine. However, if I change the function signature to
function saveChanges(target, obj, cancel)
and pass in the target object from the setClickable method:
$('.buttonB').click(function() { saveChanges(target, this, true); });
calling
setClickable(target);
doesn't seem to work and I can't figure out why.
Up until this point, I haven't done much in javascript. I recently started playing with jQuery and came across a tutorial on how to do Edit in Place on a form.
The resulting code works perfectly if you hard-code the id of your container div in the function, but I'm having issues generalizing it for an id passed in to the function.
The code starts like this:
$(document).ready(function() {
setClickable($('#editInPlace'));
});
function setClickable(target)
{
$(target).click
(
function()
{
//Do some dom manipulation
$('.buttonA').click(function() { saveChanges(this, false); });
$('.buttonB').click(function() { saveChanges(this, true); });
}
)
.mouseover(function() { $(this).addClass("editable"); })
.mouseout(function() { $(this).removeClass("editable"); });
}; //end of function setClickable
function saveChanges(obj, cancel)
{
//Handle the save changes code
//Reset the div
$(obj).parent().after('<div id="editInPlace">' + t + '</div>').remove();
//Call setClickable
setClickable($('#editInPlace'));
}
The part I'm having issues with is the last call to setClickable
inside of the saveChanges
function. If I hard-code the value of $('#editInPlace')
, it works fine. However, if I change the function signature to
function saveChanges(target, obj, cancel)
and pass in the target object from the setClickable method:
$('.buttonB').click(function() { saveChanges(target, this, true); });
calling
setClickable(target);
doesn't seem to work and I can't figure out why.
Share Improve this question asked Apr 1, 2009 at 13:26 jerhinesmithjerhinesmith 15.5k18 gold badges63 silver badges90 bronze badges 1- 1 You might want to look at the Jeditable plugin, appelsiini/projects/jeditable or the Editable plugin, arashkarimzadeh./index.php/jquery/…. – tvanfosson Commented Apr 1, 2009 at 13:34
2 Answers
Reset to default 6Change:
function setClickable(target)
{
$(target).click
(
to:
function setClickable(target)
{
target.click
(
You're passing in a wrapped set (also called a jQuery object) and then wrapped it in another wrapped set. The above fixes that. The alternate way to do this is:
$(document).ready(function() {
setClickable('editInPlace');
});
function setClickable(id)
{
$("#" + id).click
(
You see what I'm getting at?
You can always try using setClickable($(target)); to make sure you've got a jQuery object.
本文标签: jQueryjavascript object passingStack Overflow
版权声明:本文标题:jQueryjavascript object passing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741295566a2370797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论