Javascript 查找所选元素的最近链接

Javascript 查找所选元素的最近链接,javascript,jquery,html,css,Javascript,Jquery,Html,Css,这是我的HTML: <div class="action"> <input type="text" id="StudentName" /> <div></div> <div></div> <input type="text" id="item1" /> <input type="text" id="item2" /> <a href="#">

这是我的HTML:

<div class="action">
    <input type="text" id="StudentName" />
    <div></div>
    <div></div>
    <input type="text" id="item1" />
    <input type="text" id="item2" />
    <a href="#">yahoo</a>
    <input type="text" id="item3" />
    <a href="#">gmail</a>
    <input type="text" id="item4" />
    <a href="#">facebook</a>
</div>
正确的方法是什么?

您可以使用@georg建议的
$('#StudentName').parent().find('a:first')
。这将找到第一个
a
,它是父元素的子元素。 您还可以使用
$('#StudentName').sibbines('a:first')
,它将在同一DOM级别中找到第一个a


我想在link中把雅虎的名字改成abc.com,比如yahoo.com

在调用
文本
进行更改之前,先对文本使用
同级
,然后使用
过滤器

$('#StudentName')。同级('a')。筛选器(函数(){
返回$(this).text()=='yahoo';
}).text('abc.com')

试试这个:

$("#StudentName").siblings('a:first').html('yahooo');   

这将为您提供yahoo的first,那么您想做什么呢?还没有完成,
closest()
DOM向上移动,而不是在siblingsno中搜索,您需要
$(“#StudentName”).parent().find('a:first')
就。。。什么?举个例子,你到底想找到哪一个元素?我想把链接中的名称yahoo改为abc.com,比如yahoo.com
$("#StudentName").siblings('a:first').html('yahooo');