Javascript jQuery获取点击链接的ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13809872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery getting ID of clicked link
提问by user14377
I have a modal box in jQuery which I have created to display some embed code. I want the script to take the idof the link that is clicked but I can't seem to get this working.
我在 jQuery 中有一个模态框,我创建了它来显示一些嵌入代码。我希望脚本采用id单击的链接,但我似乎无法使其正常工作。
Does anyone know how I can do that or why this may be happening?
有谁知道我该怎么做或为什么会发生这种情况?
My jQuery code is:
我的 jQuery 代码是:
function generateCode() {
var answerid = $('.openembed').attr('id');
if($('#embed input[name="comments"]:checked').length > 0 == true) {
var comments = "&comments=1";
} else {
var comments = "";
}
$("#embedcode").html('<code><iframe src="embed.php?answerid=' + answerid + comments + '" width="550" height="' + $('#embed input[name="size"]').val() + '" frameborder="0"></iframe></code>');
}
$(document).ready(function () {
$('.openembed').click(function () {
generateCode();
var answerid = $('.openembed').attr('id');
$('#box').show();
return false;
});
$('#embed').click(function (e) {
e.stopPropagation()
});
$(document).click(function () {
$('#box').hide()
});
});
My mark-up is:
我的标记是:
<a href="#" id="7830" class="openembed">Embed</a>
<a href="#" id="9999" class="openembed">Embed</a>
回答by freedev
Your problem is here:
你的问题在这里:
$('.openembed')
returns an array of matched elements. Your should instead select only the clicked element.
$('.openembed')works correctly if you assing a clickevent to all elements that have this class. But on the other hand, you're unable do know which is clicked.
返回匹配元素的数组。您应该只选择单击的元素。
$('.openembed')如果您将click事件分配给具有此类的所有元素,则可以正常工作。但另一方面,您无法知道点击了哪个。
But fortunately in the body of handler function click you could call $(this).
但幸运的是,在处理程序函数的主体中单击您可以调用$(this).
$(this)will return the current (and clicked element).
$(this)将返回当前(和点击的元素)。
// var answerid = $('.openembed').attr('id'); // Wrong
var answerid = $(this).attr('id'); // Correct
// Now you can call generateCode
generateCode(answerid);
Another error is the body of generateCodefunction. Here you should pass the id of selected element. This is the correct implementation.
另一个错误是generateCode函数体。在这里,您应该传递所选元素的 id。这是正确的实现。
function generateCode(answerid) {
if($('#embed input[name="comments"]:checked').length > 0 == true) {
var comments = "&comments=1";
} else {
var comments = "";
}
$("#embedcode").html('<iframe src="embed.php?answerid=' + answerid + comments + '" width="550" height="' + $('#embed input[name="size"]').val() + '"frameborder="0"></iframe>');
}
Here I have implemented your code with the correct behavior: http://jsfiddle.net/pSZZF/2/
在这里,我用正确的行为实现了您的代码:http: //jsfiddle.net/pSZZF/2/
回答by dsgriffin
Instead of referencing the class, which will grab all members of that class, you need to reference $(this)so you can get that unique link when it is clicked.
而不是引用class,它将获取该类的所有成员,您需要引用$(this)以便在单击时获得该唯一链接。
var answerid = $(this).prop('id');
回答by apscience
$('.openembed').click(function () {
generateCode();
var answerid = $(this).attr('id');
$('#box').show();
return false;
});
Use $(this). $('.openembed') refers to multiple links.
使用 $(this)。$('.openembed') 指的是多个链接。
回答by epascarello
var answerid = $('.openembed').attr('id');
needs to be
需要是
var answerid = $(this).prop('id');
回答by jmm
Get the id when the correct anchor is clicked and pass it into your generateCode function
单击正确的锚点时获取 id 并将其传递到您的 generateCode 函数中
$('.openembed').click(function () {
var answerid = $(this).attr('id');
generateCode(answerid)
$('#box').show();
return false;
});
Change your function
改变你的功能
function generateCode(answerid) {
// dont need this line anymore
// var answerid = $('.openembed').attr('id');
回答by Christophe
The other answers are trying to fix the click() function, but your issue is actually with the generateCode function.
其他答案试图修复 click() 函数,但您的问题实际上与 generateCode 函数有关。
You need to pass the clicked element to the generateCode function:
您需要将单击的元素传递给 generateCode 函数:
$('.openembed').click(function () {
generateCode(this);
And modify generateCode:
并修改生成代码:
function generateCode(element) {
var answerid = element.id;
Of course var answerid = $('.openembed').attr('id');within the click code isn't correct either, but it doesn't seem to do anything anyway.
当然var answerid = $('.openembed').attr('id');在点击代码中也不正确,但它似乎没有任何作用。

