选择parent()在同一个链接中变得不同<a>

时间:2016-04-18 21:56:06

标签: javascript php jquery html

So here is my problem: I want to select the div with the class "read_more hidden".In the first link it selects it, but on the second one it doesn't.It says undefined..Could someone explain to me what's going on?

(I am trying to make "read more.." link on a big text coming from the database)

PHP

while($row=mysql_fetch_assoc($result))
{
    $str==NULL;
    $offset==NULL;
    $max_length==NULL;
    $str=$row['subject'];
    $max_length = 3500;
    if (strlen($str) > $max_length)
    {
        $offset = ($max_length - 3) - strlen($str);
        $str = substr($str, 0, strrpos($str, ' ', $offset)) . '<br></br><a class="clicky" href="">read more...</a>';
    }

    echo'<div class="GTextBorder" >
        <center>
        <h3>
        <em>';
        if ($row['title']==NULL)
        {
            echo 'No title';
        }
        else
        {
            echo ''.$row['title'].'';
        }

    echo '</em></h3></center><br></br>
            <div class="read_more hidden" >'.$row['subject'].'
                <a class="clickya" href=""><i>less...</i></a>
            </div>
            <div class="item" >'.$str.'</div>
        <br></br>';
    echo '</div>';
    echo '<br></br>';
}

JS

$('.clicky').click(function(e){
    e.preventDefault();
    var id=$(this).parent().parent().prev().attr("class");
    window.alert(id);
});

Some explanation: I first take the $row['subject'] which is the longtext and i check if it's more than 3500 chars.if it is then i put read more.. link so it can open.My first idea was to make two separate divs(one hidden) one normal so when read more.. is clicked the two divs would change.so read_more div would be shown and ".item" div would be hidden.But for some reason when the third long text is printed with read more.. link it just cant select the whole GTextBorder div. i don't know why.

0 个答案:

没有答案