2014-01-29 53 views
1

我正在使用kohana框架。使用jquery(kohana)的可点击的div

我试图让DIV点击可以和我不认为这将有可能与konaha做

<?php echo HTML::anchor($link .$test1->getid(). '/travel'); ?> 

我想我最好的方法将是使用jQuery windown.location.href

我现在的问题是我将如何制作

<?php echo HTML::anchor($link .$test1->getid(). '/travel'); ?> 

在jquery中工作?

我知道这通常

$(".grip_box").click(function() { 
    window.location = ""; // but this is where my problem is. as the url is different depending on the idea `$link.$test->getid` 
}); 

我一般是新来的Kohana和MVC。

<div class="general"> //this diiv just keep things in line and make sure that the grid works in the right way. 
    <div class"grip_box"> --this is the div I want to make click-able 
     <div class"couunt"> // this just shows the number of comment and will take you the comment to save you time from scrolling 
     <?php echo HTML::anchor($link . $test1->getId() . '/travel#comments', count ($travelcomment)); ?> 
     <?php echo count($countnumber); ?> 
     </div> 
      <div class="title"> 
      <?php echo HTML::anchor($link2 . $test1->getId() . '/view', $test1->getTitle()); ?> 
      </div> 
    </div> 
<div> 
+0

什么是它的输出HTML?张贴也是。 – Jai

+0

我对kohana不太熟悉,但是如果有一种方法可以为你的锚点设置一个id,那么可以这样做:$(“#you_anchor_id”)。click(function(){... – FabioG

+0

@Jai –

回答

1

对于简单的环节,所有的HTML::anchor所做的就是调用URL::site,并建立一个a标签吧。

你可以自己只是调用该方法,像这样:

... 
window.location = "<?php echo URL::site($link . $test1->getid() . '/travel') ?>" 
... 

你需要把这个JavaScript在页面本身,而不是在外部JavaScript文件,但是这可能确定。


UPDATE:这里有一个更清洁的方式做到这一点,我不能输入移动应用昨晚...

这样做最彻底的方法可能是使用data -attributes :

HTML

<div class="general"> 
    <div class"grip_box" data-url="<?php echo URL::site($link . $test1->getid() . '/travel') ?>"> 
     <div class"count"> 
     <?php echo HTML::anchor($link . $test1->getId() . '/travel#comments', count ($travelcomment)); ?> 
     <?php echo count($countnumber); ?> 
     </div> 
      <div class="title"> 
      <?php echo HTML::anchor($link2 . $test1->getId() . '/view', $test1->getTitle()); ?> 
      </div> 
    </div> 
<div> 

的JavaScript:

$(".grip_box").click(function() { 
    if ($(this).attr('data-url')) { 
     window.location = $(this).attr('data-url'); 
    } 
}); 

这是最灵活的,因为你可以用它在页面上的任何元素很少改变的代码。

或者,你可以只在ID存储在一个data -attribute这样的:

... 
<div class"grip_box" data-itemid="<?php echo $test1->getid() ?>"> 
... 

,然后建立在JavaScript的其余部分:

... 
window.location = "BEGINNING_OF_URL" + $(this).attr('data-itemid') + "/travel"; 
... 
+0

感谢Moshe,但我不会使用它,因为我喜欢将js文件放在js文件夹中而不喜欢把它与一个php文件混合在一起,但是我会用jquery来获得'title'类中的URL –

+0

@SarahJames看到我的更新了,你不应该使用'title'属性,因为它不是为了这个, 'data'-attributes是专门为此设计的。 –

+0

谢谢。我使用jquery代替它。 –