【问题标题】:jQuery Selector Issue With Anchor Tags and Hiding Div带有锚标记和隐藏 Div 的 jQuery 选择器问题
【发布时间】:2011-11-12 11:26:50
【问题描述】:

我正在使用 jquery。我有一个在 xslt 中动态生成的锚标记。每个锚点都必须打开一个隐藏的 div 并在 onclick 事件中传递不同的值。

在隐藏的 div 中,我有 2 个不同的按钮,一个用于取消操作,另一个用于确认。这个想法是:当我单击确认按钮时,previus 锚(之前单击以显示隐藏的 div)获得其他样式(我使用 addClass,工作正常)。我在 click 函数上传递值 $(this) 来创建引用,它运行良好......

问题:当我点击不同的锚点(以显示 div)时,前一个被点击获取新值。例如,如果我单击“a”按钮,显示 div,单击取消(没有任何反应,那很好),然后单击“b”按钮并单击确认,“a”和“b”得到风格。

我只需要在所选按钮上显示样式,但不知道如何做到这一点。

我的代码:

function showInfo(team, selectName, selectId, temps, thisid){
    //Create a function that will recieves some values            
 var showInfoLink = thisid; //Asigh a new variable the value of thisid ( $(this) )
 showInfoLink.removeClass("highlighted");

 //Show the component
 $("#com_advanceBet").css('z-index' , '1' );

//in the component is a select, it will show the name on the anchor of the selected item                
 $("select").change(function () {
   var str = "";
   $("select option:selected").each(function () {
   str += $(this).text() + " ";
   });
   showInfoLink.html(str);
 })
 .change();

//once i click here the anchor will have this class 
   $('#com_continueBet').click(function(){
   showInfoLink.removeClass('nostyle');
   showInfoLink.addClass('highlighted');

 //hide the component
  $("#com_advanceBet").css('z-index' , '-999999');
  });               
}

这是锚点:

<a href="#" onclick="showInfo('{$team}', '{$selectName}', '{$selectId}', '{$temps}', $(this) ); " class="nostyle" ><xsl:value-of select="$pt" disable-output-escaping="yes"/><span class="test" style="font-size:0.65em"><xsl:value-of select="$odds" disable-output-escaping="yes"/></span></a>

在锚点中,我将$(this) 传递给函数,但出于某种原因,它具有所有先前单击的锚点的值。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    我在 jsFiddle 上创建了一个模拟版本:http://jsfiddle.net/dPZJc/6/

    它被简化了很多,并且似乎可以正常工作。

    【讨论】:

    • 谢谢,工作但不是我希望的那样.. 我的意思是,我不能将 id 添加到锚点,因为它们是在没有 id 的情况下动态生成的,我有很多 id 在单击时运行相同的功能,问题是我有很多锚点指针($(this))超出范围(我猜)并且出现了一些问题......
    • 它们是由 JavaScript 还是服务器端脚本语言生成的? id 应该是无关紧要的;正如您在脚本中看到的那样,它们只是用于测试以确保 $(this) 处于正确的上下文中。如果您使用 alert(thisid.attr('id'); 取消注释函数的第一行,您将看到它确实引用了正确的对象(在本例中为 DOM 元素)。
    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 2012-08-04
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多