Javascript在循环中悬停时获取参数

javascript get parameters when hover href inside a loop

本文关键字:获取 参数 悬停 循环 Javascript      更新时间:2023-09-26

我有以下问题:

var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object
$(function() {
$('a.popper').hover(function(e) {

 if(xmlhttp) {
xmlhttp.open("GET","DokterWeek_KlantoverzichtServlet?" + $("a.popper").prop("href").split("?")[1],true);//gettime will be the servlet name
xmlhttp.onreadystatechange  = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}

上面你可以看到,当我把鼠标悬停在一个href class="popper"上面时,我得到了servlet的参数。但问题是,它总是在foreach循环中获得最后一个"a href"参数…

 <c:forEach items="${row}" var="cell">
<a href="./DokterWeek_KlantoverzichtServlet?AfspraakID=${cell.afspraakId}&Id=${cell.id}&KlantId=${cell.klant.id}" 
class="popper" data-popbox="pop1">
<c:forEach items="${row}" var="cell">

是否有任何javascript或jquery的可能性,我可以得到一个href值LIVE当我悬停在它…当我将鼠标悬停在这些链接上时,我的浏览器看到参数在变化,但javascript不会获取它。Javascript接受循环中的第一个或最后一个ahref…

请帮助我,我一直在寻找一个解决方案超过2天:(

问题是你没有得到你悬停的元素的href值。你可以用$(this)来做。在您的示例中,您使用$("a.popper"),它获取所有a.popper元素,从而选择最后一个href.

在你的例子中:

var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object
$(function() {
$('a.popper').hover(function(e) {
    var theElementYourHovering = $(this),
        currentHref = theElementYourHovering.attr('href');
    // Do your stuff here
}

在这种情况下,您需要获得悬停元素的href。这可以使用$(this)

来完成
xmlhttp.open("GET","DokterWeek_KlantoverzichtServlet?" + $(this).prop("href").split("?")[1],true);//gettime will be the servlet name

注意:由于您正在使用jQuery,请使用$.ajax()实用程序进行ajax调用,而不是使用原始XmlHTTPRequest