函数如何找到最初调用该函数的锚点的父元素?
好吧,光是这个问题就让我头晕目眩。
我有一个正在调用函数的锚标记:
<a href="#" id="addPerson" onClick="addPerson(); return false;">Add a Guest</a>
仅供参考,这是正在调用的函数:
function addPerson() {
//current keeps track of how many rows we have.
current++;
console.log($(this).parent('form').attr('id'));
var strToAdd = '<div class="row">\
<div class="column grid_2">\
<label for="two-guests-name'+current+'">Guest '+current+':</label>\
</div>\
<div class="column grid_5">\
<input name="two-guests-name'+current+'" type="text" id="two-guests-name'+current+'" value="Name" onfocus="RemoveFormatString(this, \'Name\')" /> \
<input name="two-guests-age'+current+'" type="text" class="guestage digits" id="two-guests-age'+current+'" value="Age" size="4" maxlength="3" onfocus="RemoveFormatString(this, \'Age\')" />\
</div>\
</div>'
$('#numberguests').append(strToAdd)
};
我希望允许此函数在单个页面上的多个表单上工作。所以我的想法是在树中返回到父表单元素,获取其 id 并使用它(以某种方式)允许该函数在页面中的多个表单上工作。
您可以看到我尝试使用快速 console.log 来测试我的想法,但它不断返回“未定义”。我还尝试在锚标记本身中运行 console.log,但它也是“未定义”。
我测试了 .parents() ,它在锚标记中完美运行,但在函数 iteslf 中不起作用。
有什么建议吗?
Ok, the question alone is making my head spin.
I have an anchor tag that is calling a function:
<a href="#" id="addPerson" onClick="addPerson(); return false;">Add a Guest</a>
Just for reference, this is the function that's being called:
function addPerson() {
//current keeps track of how many rows we have.
current++;
console.log($(this).parent('form').attr('id'));
var strToAdd = '<div class="row">\
<div class="column grid_2">\
<label for="two-guests-name'+current+'">Guest '+current+':</label>\
</div>\
<div class="column grid_5">\
<input name="two-guests-name'+current+'" type="text" id="two-guests-name'+current+'" value="Name" onfocus="RemoveFormatString(this, \'Name\')" /> \
<input name="two-guests-age'+current+'" type="text" class="guestage digits" id="two-guests-age'+current+'" value="Age" size="4" maxlength="3" onfocus="RemoveFormatString(this, \'Age\')" />\
</div>\
</div>'
$('#numberguests').append(strToAdd)
};
I'd like to allow this function to work on multiple forms on a single page. So my thinking was to travel back in the tree to the parent form element, get its id and use that (somehow) to allow the function to work on multiple forms in the page.
You can see I tried using a quick console.log to test my idea, but it kept coming back "Undefined". I also tried running the console.log in the anchor tag itself, but it also was "Undefined".
I tested .parents() and it works perfectly in the anchor tag, but not in the function iteslf.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该将 $(this) 传递给添加父母,然后您将在您的函数中拥有适当的范围。所以:
然后在函数中使用:
You should pass $(this) into add parents and then you'll have the proper scope to work with in your function. So:
and then in the function use:
不要使用
onclick
属性来使用 jQuery 事件绑定。由于您没有提供有关标记的任何信息,因此您需要自己填写选择器Don't use the
onclick
attribute use jQuery event binding. As you didn't provide any info about the markup you need to fill in the selector yourselfthis
上下文是不同的。将 onclick 更改为这样:这将使函数中的
this
与锚标记中的this
匹配。或者更好的是,根本不使用 onclick 属性,而是使用 jQuery 事件处理程序附件,如下所示:
并且您的锚标记上不会有任何单击处理程序:
The
this
context is different. Change the onclick to this:That will make the
this
in the function match up with thethis
in the anchor tag.Or better yet, don't use an onclick attribute at all, but use a jQuery event handler attachment, like this:
And your anchor tag would not have any click handler on it:
将parents() 与选择器一起使用(这将向上搜索所有父级,直到找到匹配项)。例如
Use parents() with a selector (this will search upward through all parents until the match i found). e.g.
将 id 作为参数传入。例如
Pass in the id as a parameter. Eg