A
A
Andrey Tokmakov2014-04-25 05:58:03
JavaScript
Andrey Tokmakov, 2014-04-25 05:58:03

Jquery doesn't hang handler on link in yii menu?

There is a regular menu

<?php $this->widget('zii.widgets.CMenu',array(
  'items'=>array(
    array('label'=>'Home', 'url'=>array('#')),
    array('label'=>'About', 'url'=>array('#about')),
    array('label'=>'About2', 'url'=>array('#about2')),
  ),
  'id'=>'top-menu',
  'htmlOptions'=>array('class'=>'top-menu'),
)); ?>

add to layout
<script type="text/javascript">
$(function(){
   $('a').click(function(){
     alert('Работает');
        return false; 
   }); 
});
</script>

As a result, when you click on a link in the menu, you simply go to the anchor, but if you insert the link manually into any part of the page, for example, <a href="#about2">я ссылка не из меню</a>then the alert works fine.
Again, if you replace it $('a').click(function(){with $('li').click(function(){then, when you click past the link to li, the alert fires.

Already tried and $('li a')and $('.top-menu li a')and for everyone

Why is the handler not hung on the link in the menu?

UPD:
That's it, the issue was resolved by replacing it
$('a').click(function(){with $('a').live('click',function(){
Sam blunted

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Papa, 2014-04-25
Stifflera @PapaStifflera

preventDefault()
Read more in the documentation: api.jquery.com/event.preventdefault

A
Andrey Yelpaev, 2014-04-25
@andreyelpaev

Try this way

$('a').on("click", function(){
     alert('Работает'); 
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question