Вопрос

I am using kohana framework.

I am trying to make div click able and i dont think it will be possible to do it with konaha

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

I think my best way will be with jquery windown.location.href

My question now is how will I make

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

work in jquery?

I know it is normally

$(".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`
});

I am new to kohana and MVC in general.

<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>
Это было полезно?

Решение

For simple links, all that HTML::anchor does is it calls URL::site and builds an a tag with it.

You can just call that method yourself, like this:

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

You will need to put this JavaScript in the page itself and not in an outside JavaScript file, but that's probably OK.


UPDATE: Here's a cleaner way to do it that I couldn't type in the mobile app last night...

The cleanest way to do this is probably to use 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');
    }
});

This is the most flexible because you can use it for any elements on the page with very little change to the code.

Alternatively, you could store just the id in a data-attribute like this:

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

and then build the rest in the JavaScript:

...
window.location = "BEGINNING_OF_URL" + $(this).attr('data-itemid') + "/travel";
...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top