使用Jquery(kohana)的可点击div

时间:2014-01-29 11:51:03

标签: jquery kohana-3

我正在使用kohana框架。

我正在努力让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>

1 个答案:

答案 0 :(得分:1)

对于简单链接,HTML::anchor所做的只是调用URL::site并使用它构建a标记。

您可以自己调用该方法,如下所示:

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

您需要将此JavaScript放在页面本身而不是外部JavaScript文件中,但这可能没问题。


更新:这是一种更干净的方法,我昨晚无法在移动应用中输入...

最简洁的方法是使用data - 属性:

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 - 属性中,如下所示:

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

然后在JavaScript中构建其余部分:

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