JavaScript 隐藏显示

发布于 2024-11-07 12:47:37 字数 954 浏览 0 评论 0原文

我正在尝试用 javascript 做一个简单的隐藏和显示功能(不是 Jquery,我想学习);是的,我还在学习。我想做的是 Facebook 和其他应用程序,当你将鼠标悬停在新闻提要文章上时,会显示一个小 X,可以作为删除该文章的选项。我试图在表格(表格单元格)上执行此操作,

这是我的标题函数:

function showHide(id) {
    if(document.getDocumentById(id).style.visibility == 'hidden')
        document.getDocumentById(id).style.visibility = 'visible';
    else
        document.getDocumentById(id).style.visibility = 'hidden';
}

在正文(php)中:

echo '<tr>';
        echo '<td class="managealbum_delete" id="managealbum_delete'.$x.'">X</td>';
        echo '<td>' . $album->title . '</td>';
        echo '<td>' . $album->caption . '</td>';
        echo '<td style="border: 1px solid black;" onMouseOver="showHide('."'".'managealbum_delete'.$x."'".');" onMouseOut="showHide('."'".'managealbum_delete'.$x."'".');">' . $album->media . '</td>';
        echo '</tr>';

I'm trying to do a simple hide and show function in javascript (NOT Jquery please, I want to learn); and yes I'm still learning. What I'm trying to do is Facebook and other apps that when you hover on a news feed article, there's a small X that shows that serves as an option to delete that. I'm trying to do that on tables (table cell)

here's my header function:

function showHide(id) {
    if(document.getDocumentById(id).style.visibility == 'hidden')
        document.getDocumentById(id).style.visibility = 'visible';
    else
        document.getDocumentById(id).style.visibility = 'hidden';
}

and in the body (php):

echo '<tr>';
        echo '<td class="managealbum_delete" id="managealbum_delete'.$x.'">X</td>';
        echo '<td>' . $album->title . '</td>';
        echo '<td>' . $album->caption . '</td>';
        echo '<td style="border: 1px solid black;" onMouseOver="showHide('."'".'managealbum_delete'.$x."'".');" onMouseOut="showHide('."'".'managealbum_delete'.$x."'".');">' . $album->media . '</td>';
        echo '</tr>';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2024-11-14 12:47:37

该函数是 getElementById,而不是 getDocumentById

function showHide(id) { 
    if(document.getElementById(id).style.visibility == 'hidden')    
        document.getElementById(id).style.visibility = 'visible'; 
    else 
        document.getElementById(id).style.visibility = 'hidden'; 
}

另外,您可以稍微缩短它,并检查以确保找到一个元素,如下所示:

function showHide(id) {
    var el = document.getElementById(id);
    if( el && el.style.visibility == 'hidden')    
        el.style.visibility = 'visible'; 
    else 
        el.style.visibility = 'hidden'; 
}

The function is getElementById, not getDocumentById

function showHide(id) { 
    if(document.getElementById(id).style.visibility == 'hidden')    
        document.getElementById(id).style.visibility = 'visible'; 
    else 
        document.getElementById(id).style.visibility = 'hidden'; 
}

Also, you can shorten it a bit, and check to make sure an element was found like this:

function showHide(id) {
    var el = document.getElementById(id);
    if( el && el.style.visibility == 'hidden')    
        el.style.visibility = 'visible'; 
    else 
        el.style.visibility = 'hidden'; 
}
风筝有风,海豚有海 2024-11-14 12:47:37

好吧,如果您确实想隐藏它而不仅仅是删除其内容,您应该使用:

document.getDocumentById(id).style.display = 'none';

要显示它(TD),请使用:

document.getDocumentById(id).style.display = 'table-cell';

http://www.w3schools.com/cssref/pr_class_display.asp

Well, if you really want to hide it and not just erase its content you should use:

document.getDocumentById(id).style.display = 'none';

And to show it (TDs), use:

document.getDocumentById(id).style.display = 'table-cell';

http://www.w3schools.com/cssref/pr_class_display.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文