English 中文(简体)
使用 $( this) 获取点击元素
原标题:Getting the clicked element using $(this)

我有一个愚蠢的问题,希望你能帮我解决它。我有一些“A”元素,我想在点击时添加一个活跃的课。

我试过这个:

<a href="javascript:mifunction()"></a>

js:
function mifunction(){
 $(this).addClass( active );
}

但它不工作, 所以我尝试了这一点:

<a></a>

js:
$( a ).click(function() {
  $(this).addClass( active );               
});

它也不管用。它只是在我兼并时起作用。 但用户必须按下这个元素, 这不是一个选项 :

<a href="javascript:mifunction()"></a>
js:

function mifunction(){
 $( a ).click(function() {
   $(this).addClass( active );              
 });
}

你知道怎么解决这个问题吗?

非常感谢!

最佳回答

它在这里与你的第二个例子一起工作:

您可能需要将jQuery 代码包在 :

$(function() {

  $( a ).click(function() {
     $( a.active ).removeClass( active ); // you may also want to remove previous
     $(this).addClass( active );               
  });

});

确保您在 HTML 中包含 jQuery 库路径 :

<script type="text/javascript" src="/path/jQuery.min.js"></script>

<强>OR:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

EDIT:

(Links are added dynamically)
$(function() {

  $( a ).on(  click , function() {
     $( a.active ).removeClass( active ); // you may also want to remove previous
     $(this).addClass( active );               
  });

});
问题回答

你和DOM和JavaScript 混合和匹配新的新旧工作方式

缩略

$( a ).click(function() {
    $(this).addClass( active );
}

应该很好地工作。

将它放入 $( document).ready( 函数( ) ) ;

所以,从字面上来说,它应该是这样的:

<script type="text/javascript">
    $(document).ready(function() {
        $( a ).click(function() { 
            $(this).addClass( active );
        });
    });
</script>
<a>Click me</a>


Also, if you re just trying to style.. you can do it via CSS
For Active: a:active { color: orange; }
For Visited: a:visited { color:green; }

你的问题必须存在于别的地方 因为这样很好:

$( a ).click(function () {
   $(this).addClass( active ); 
});​

如果您正在动态地创建锚, 您应该使用 < code>. on () 代替 :

$( #container ).on( click ,  a , function () {
    $(this).addClass( active ); 
});

.on() :>http://jsfidd.net/jbabey/bqg FNF/1/

.on() 的文档:http://api.jquery.com/on/

尝试在下面做它,

<a class="link" href="javascript:void(0)"></a>

$(function () {
   $( .link ).click (function () {
      $(this).addClass( active );
   });
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签