jQuery 如何获取点击元素的rel值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1482876/
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
how to get rel value of clicked element
提问by Phil Hymanson
hi im trying to get the rel value of a button that is click:
嗨,我正在尝试获取单击按钮的 rel 值:
function forwardStep()
{
$("#forwardStep").bind("click", function(){
var page = $(this).attr('rel');
thats pretty much the basis but is return "undifined". Do you know why?
这几乎是基础,但返回“未定义”。你知道为什么吗?
Regards Phil Hymanson
问候菲尔Hyman逊
回答by Mottie
If you're using a button with an ID, then you shouldn't have to get any extra data from the rel attribute since the function is unique to your button.
如果您使用带有 ID 的按钮,那么您不必从 rel 属性中获取任何额外数据,因为该功能对于您的按钮是唯一的。
But if you have other reasons, then you just need to make sure your button itself has a rel attribute defined, e.g.
但是如果你有其他原因,那么你只需要确保你的按钮本身定义了一个 rel 属性,例如
<input id="forwardStep" rel="test" type="button" value="forward" />
and your function is closed properly
并且您的功能已正确关闭
$(document).ready(function(){
$('#forwardStep').bind('click',function(){
var page = $(this).attr('rel');
alert(page);
})
})
回答by meder omuraliev
Because it wasn't ever defined? Where's the html? Are you sure you're targeting the right element?
因为它从来没有被定义过?html在哪里?你确定你的目标是正确的元素吗?
回答by Lance Fisher
$(this) will give you whatever element has the id of "#forwardStep", so for this example to work it should be a link or anchor, but you said it was a button.
$(this) 会给你任何具有 "#forwardStep" id 的元素,所以对于这个例子来说,它应该是一个链接或锚点,但你说它是一个按钮。
I don't think a button can have a rel attribute. If you are trying to get the rel attribute from a link you'll need to selct that link rather than "this". e.g. $('#theLinkId').attr('rel').
我不认为按钮可以具有 rel 属性。如果您尝试从链接获取 rel 属性,则需要选择该链接而不是“this”。例如 $('#theLinkId').attr('rel')。
You should add the HTML to this question.
您应该将 HTML 添加到此问题。