使用jQuery获取以前的HTML锚定符(< a>)的href值 [英] Get the href value of a previous HTML anchoer, <a>, using jQuery

查看:169
本文介绍了使用jQuery获取以前的HTML锚定符(< a>)的href值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取< a>的href值,这是我当前单击的按钮的前一个元素.如何获得?

I am trying to get the value of href of <a> that is the previous element to my current button that is clicked. How do I get it?

这是我的代码.

<a href="#activityNotes[0].noteText">
    <input value="Add Another" name="addAnotherNote" type="submit" class="addAnother secondary" onclick="addAnotherNote();"/>
</a>

jQuery:

$(".addAnother").click(function() {
    var jump = $(this).prev("a").attr("href");
});

当我尝试运行它时,我得到

When I try running this, I get

跳跃值未定义.

jump value undefined.

为什么会这样?是因为我试图获得一个#在前面的锚点吗?

Why is this happening? Is it because I'm trying to get an anchor with # in front?

http://jsfiddle.net/5Twfc/2/

实际上,当我单击添加"按钮时,我正在尝试使显示与name/id = activityNotes[0].noteText锚定.这就是我在< a>中使用此输入按钮的原因.另外,我运行了几个脚本后就开始提交表单.

Actually I am trying to get the display to anchor section with name/id = activityNotes[0].noteText when I click on the add button. That is the reason I have this input button inside the <a>. Also, I am doing a form submit after I run few scripts.

$(".addAnother").click(function() {
    var jump = $(this).parent().attr("href");
    var new_position = $('#'+jump).offset();
    window.scrollTo(new_position.left,new_position.top);
    return false;
});

推荐答案

您的a包含input. prev获得上一个同级.在这种情况下,您需要获得父母.这是根据您的jsFiddle工作的内容:

Your a encloses input. prev gets previous sibling. In this case, you need to get the parent. Here's what will work based on your jsFiddle:

$(".addAnother").click(function() {
    var jump = $(this).parent().attr('href');
    $('#printJumpValue').text('' + jump);
})

和一个实时示例:

这篇关于使用jQuery获取以前的HTML锚定符(&lt; a&gt;)的href值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆