-2

我熟悉 php,但是 js 的认真学习者,我尝试了 3 种方法来实现我的目标,我看到下面使用的方法似乎最容易实现,所以我可以实现我的目标,但由于某种原因它不起作用. 我只包含了不工作的特定函数的必要位,这一切都在基于数据库中的条目的 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>
4

2 回答 2

0

关注点分离:避免直接在 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

于 2013-04-09T23:10:35.097 回答
0

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

于 2013-04-09T23:17:20.833 回答