显示隐藏JS div css

时间:2013-04-09 22:58:11

标签: javascript css

我熟悉php,但是js中的一个认真的学习者,我已经尝试了3种方法来实现我的目标,我已经看到下面使用的方法似乎最容易实现,所以我可以实现我的目标,但由于某种原因它不起作用。我只包含了不起作用的特定函数的必要位,这一切都在基于db中的条目的while循环中工作,与{$ page_trackid}分开以区分每个函数和它的链接。

我理解可以使用'this'样式函数,但正如我所说的那样,对于js来说,我只是试图在我扩展到更复杂之前实现必要。

这里有什么东西我完全错了吗?

干杯

<head>

    <style>
        .hidden {display:none;}
        .visible {display:block;}

        .subtext_img {
            width: 100px; height: 100px; padding-top: 10px; padding-right: 10px; float: right;
        }

        .subtext {
            padding-left: 10px;
        }
        .arrow_box { padding-left: 100px; position: absolute; z-index: 100; background: #88b7d5; border: 4px solid #c2e1f5; width: 580px; height: 120px;} .arrow_box:after, .arrow_box:before { left: 100%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; } .arrow_box:after { border-color: rgba(136, 183, 213, 0); border-left-color: #88b7d5; border-width: 10px; top: 30%; margin-top: -10px; } .arrow_box:before { border-color: rgba(194, 225, 245, 0); border-left-color: #c2e1f5; border-width: 16px; top: 30%; margin-top: -16px; }


    </style>
</head>
<body>


    <?
    $data = mysql_query("SELECT * FROM user_Uploaded_Tracks WHERE username = '$user' ORDER BY datetime DESC")
            or die(mysql_error());

    $page_trackid = '1'; //reset page track id
    // get a whole bunch of data, track info & user info based on some cross referencing via id
    while ($info = mysql_fetch_array($data)) {

        $db_trackid = $info['id'];
        $username = $info['username'];

        //data 2 track info based on db track id
        $data2 = mysql_query("SELECT * FROM user_Uploaded_Tracks WHERE id = '$db_trackid' LIMIT 1")
                or die(mysql_error());

        $info2 = mysql_fetch_array($data2);


        //data 3 is user profile data based on db track id
        $data3 = mysql_query("SELECT * FROM users WHERE username = '$username' LIMIT 1")
                or die(mysql_error());

        $info3 = mysql_fetch_array($data3);

        //data 4 profile image based on user id, track uploader*
        $data4 = mysql_query("SELECT * FROM user_profile_pic WHERE username = '$username' ORDER BY datetime DESC LIMIT 1")
                or die(mysql_error());

        $info4 = mysql_fetch_array($data4);

        echo "
        <script>
            function showbox(userinfo_{$page_trackid}){
                document.getElementById(userinfo_{$page_trackid}).style.visibility='visible';
            }

            function hidestuff(userinfo_{$page_trackid}){
                document.getElementById(userinfo_{$page_trackid}).style.visibility='hidden';
            }
        </script>
        <div id='userinfo_{$page_trackid}'  class='arrow_box' style='display: none;'>  <img src='" . $info4['Image'] . "' class='subtext_img'>
            <h2 class='subtext'><a href='http://XXXX/XXX/" . $info2['username'] . "'>" . $info2['username'] . "</a></h2>
            <p class='subtext'>" . $info3['user_title'] . "</p>
            <p class='subtext'><a href='" . $info3['website_link'] . "' target='_blank'>" . $info3['website_link'] . "</a>

            </p>

        </div>";

        echo "
            <div style='position: absolute; z-index: 1; width: 20px; height: 20px; padding-top: 50px; padding-left: 699px;'>
                <a href='javascript:showbox_{$page_trackid}()'><img style='height: 20px;' alt='Track stats' src='http://XXXXXX/play1/skin/user-profile2.png' style=''></a>
            </div>";
    }
    ?>
</body>

2 个答案:

答案 0 :(得分:0)

Separation of concerns:避免直接在javascript中操纵元素的样式。造型属于css。

这就是我要做的事情:

CSS

.hidden {
    display: none;
}

使用Javascript:

// show an element
document.getElementById("my-id").classList.remove("hidden");

// hide an element
document.getElementById("my-id").classList.add("hidden");
旧浏览器不支持

classList,因此请确保在https://developer.mozilla.org/en-US/docs/DOM/element.classList

包含polyfill

答案 1 :(得分:0)

您的问题似乎是在HTML中将 display 属性设置为 none ,但在脚本中您正在设置visibility属性。他们是不同的东西。在两个地方设置一个或另一个。