简体   繁体   English

jQuery:监听点击href

[英]Jquery: listen for click on href

I have the following HTML code, which I am unable to change: 我有以下HTML代码,无法更改:

<div id="stepbystep" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
  <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
    <li class="ui-state-default ui-corner-top">
      <a href="#step-1">
        <span>Step 1</span>
      </a>
    </li>
    <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
      <a href="#step-2">
        <span>Step 2</span>
      </a>
    </li>
    <li class="ui-state-default ui-corner-top">
      <a href="#step-3">
        <span>Step 3</span>
      </a>
    </li>
</ul>
</div>

I would like to use JQuery to listen for a click on one of the hrefs and pop up the href's destination. 我想使用JQuery侦听对href之一的单击,然后弹出href的目的地。 This is the code I have so far, but I am getting the error TypeError: $(...) is null . 这是我到目前为止的代码,但出现错误TypeError: $(...) is null I think the problem is with my selector $('div#stepbystep ul li a') . 我认为问题出在我的选择器$('div#stepbystep ul li a')

var tabCheck = {
  ready : function() {
    $('div#stepbystep ul li a').click(
      function($e) {
        $e.preventDefault();
        window.open(this, 'FavoriteLink', '');
        alert(this.href)
      }
    );
  }
};

$(document).ready(tabCheck.ready);

Thanks for any help 谢谢你的帮助

The problem was because I was using Joomla, so had to use jQuery instead of $ as Rob Schmuecker guessed in his comment. 问题是因为我使用的是Joomla,所以不得不使用jQuery而不是Rob Schmuecker在他的评论中猜测的$ I can't see a way to mark a comment as an answer, but thanks! 我看不到将评论标记为答案的方法,但谢谢!

It still isn't working because the alert is not popping up, but I'll raise a different question for that. 它仍然无法正常工作,因为没有弹出警报,但是对此我将提出另一个问题。

try: 尝试:

 $('#stepbystep ul li a')

you don't need the '$' on $e either... 您也不需要$ e上的“ $” ...

and maybe add a jsfiddle page? 也许添加一个jsfiddle页面?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM