javascript Jquery - 使用“this”获取属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9608108/
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 - Get Attribute using "this"
提问by user982853
This may be something very easy but i can't seem to get this to work and im not sure why. I have jquery installed and i am trying to get an attribute of "this" element when i click on it. Right now my code looks like this:
这可能是一件非常简单的事情,但我似乎无法让它发挥作用,我不知道为什么。我安装了 jquery,当我点击它时,我试图获取“this”元素的属性。现在我的代码是这样的:
url = $(this).attr("href")
When I call this function by clicking on a link, it tells me that the var "url" is undefined. So obviously it is not picking up the "this" when i click on the link. I am trying to pass the href of an anchor tag to use as my variable.
当我通过单击链接调用此函数时,它告诉我 var "url" 未定义。所以很明显,当我点击链接时,它没有选择“这个”。我正在尝试传递锚标记的 href 以用作我的变量。
What am i overlooking? Again, i know this is something very simple but i can't seem to figure it out so thank you for taking the time to help me.
我在俯瞰什么?同样,我知道这是一件非常简单的事情,但我似乎无法弄清楚,所以感谢您花时间帮助我。
Thanks.
谢谢。
<script type="text/javascript">
url = "push1";
$("a").live("click", function(event) {
event.preventDefault();
url = $(this).attr("href");
})
$.ajax({
type: "get",
url: "/"+url+".php",
data: "",
dataType: "html",
success: function(html){
jQuery('#Right_Content').hide().html(html).fadeIn(1000);
},
})
;
</script>
html:
html:
<body>
<a href="push1" >Image 1</a>
<a href="push2" >Image 2</a>
<div id="Right_Content"></div>
</body>
回答by Shyju
This should work for you
这应该适合你
$(function(){
$(".link").click(function(){
var url=$(this).attr("href");
alert(url);
return false;
});
});?
Assuming you are targeting all anchor tags with a css class called "link"
假设您使用名为“link”的 css 类定位所有锚标记
Here is the working example : http://jsfiddle.net/L99mM/2/
这是工作示例:http: //jsfiddle.net/L99mM/2/
Edit: As per your code posted in the question
编辑:根据您在问题中发布的代码
You should call preventDefault after your ajax call. and there is closing brackets should be after ajax call
您应该在 ajax 调用后调用 preventDefault。并且在ajax调用之后应该有右括号
$("a").live("click", function(event) {
var targeturl = $(this).attr("href");
$.ajax({
type: "get",
url: "/"+targeturl +".php",
data: "",
dataType: "html",
success: function(html){
jQuery('#Right_Content').hide().html(html).fadeIn(1000);
}
}); // closing for ajax
event.preventDefault();
}); // closing for click event binding
回答by Dunning-Kruger
<script type="text/javascript">
var url = "push1";
function getContent(){
$.ajax({
type: "get",
url: "/"+url+".php",
data: "",
dataType: "html",
success: function(html){
jQuery('#Right_Content').hide().html(html).fadeIn(1000);
});
}
$("a").live("click", function(event) {
event.preventDefault();
url = $(this).attr("href");
getContent();
});
getContent();
</script>
回答by Evert
you don't need "this" for this, you need the target of the event:
你不需要“这个”,你需要事件的目标:
$("a").live("click", function(event) {
event.preventDefault();
url = event.target.href;
})
working fiddle
工作小提琴