使用 JQuery 在单击表中的 tr 时创建弹出窗口

Using JQuery to create a pop-up when clicking on a tr in a table

本文关键字:创建 窗口 tr JQuery 单击 使用      更新时间:2023-09-26

>我在 jsp 中有一个表,想在单击表时创建一个弹出窗口。包括一个带有代码的jsfiddle,并尝试将javascript与特定行连接起来,但尚未成功创建弹出窗口。

这个小提琴是一个示例 - 我目前拥有的代码包含一个 java for 循环,使用数据库中的特定信息创建每个 tr。

<tr OnClick="display('test');">
      <td><strong>showSpeed</strong></td>
      <td>15</td>
      <td>The speed of the show/reveal</td>
    </tr>
 <script>function display(test) {
        //display a pop up?
    }</script>

https://jsfiddle.net/y2y1w24L/1/

谢谢。

您可以使用 JQuery UI 显示一个漂亮的弹出窗口。此代码是将您的代码与此处的示例合并而成的:http://jqueryui.com/dialog/

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <!--<link rel="stylesheet" href="/resources/demos/style.css">-->
</head>
<body>
    <script>
        function display(test) {
            $("#dialog").dialog();
        }
    </script>
    <table>
        <tr onclick="display('test');">
            <td><strong>showSpeed</strong></td>
            <td>15</td>
            <td>The speed of the show/reveal</td>
        </tr>
    </table>
    <div id="dialog" title="Basic dialog" style="display:none">
        <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
    </div>
</body>
</html>

这是一种选择。您还可以查看此处的引导模式:http://getbootstrap.com/javascript/#modals

在你的函数"display"中,event.target会给你一个引用,引用用户点击的任何内容:

function display(test) {
    alert(event.target.outerHTML);
}

https://jsfiddle.net/y2y1w24L/2/

您应该能够使用它来显示相应的弹出窗口。

愿这对你有所帮助

var PopUP=document.createElement("div");
PopUP.className="PopUP";
PopUP.innerHTML="<div id='mainDivBack' style='display:block;'><div id='PopUpDiv' style='display:block;'>"+" Welcome"+ "<input type='button' value='x' id='btnClose' style='display:block;' onclick='popClose();'/></div></div>";
document.body.appendChild(PopUP);
function popClose() {
        document.getElementById("btnClose").style.display = 'none';
        document.getElementById("PopUpDiv").style.display = 'none';
        document.getElementById("mainDivBack").style.display = 'none';
    }
function display(test) {
        PopUP.innerHTML="<div id='mainDivBack' style='display:block;'><div id='PopUpDiv' style='display:block;'>"+test+ "<input type='button' value='x' id='btnClose' style='display:block;' onclick='popClose();'/></div></div>";
        document.getElementById("btnClose").style.display = 'block';
        document.getElementById("PopUpDiv").style.display = 'block';
        document.getElementById("mainDivBack").style.display = 'block';
}

和为此的 CSS #btnClose { 背景:无 重复滚动 0 0 #dd2904; 边框:1px 实心 #c13031; 颜色:白色; 字体大小:18px; 字体粗细:粗体; 边距:5px; 填充: 0 1px 4px; 位置:绝对; 右:10px; 顶部: 0; } #btnClose:悬停 { 背景:无 重复滚动 0 0 #ff4565;

    }
    #PopUpDiv
    {
        background: none repeat scroll 0 0 lightgray;
        border: 1px solid gray;
        border-radius: 8px;
        box-shadow: 1px 1px 12px 3px white;
        font-family: "Lucida Console" ,arial;
        height: 70px;
        padding: 35px;
        position: absolute;
        width: 300px;
    }
    #mainDivBack
    {
        background: radial-gradient(lightgray, transparent) repeat scroll 0 0 transparent;
        height: 100%;
        left: 0;
        padding: 20% 25%;
        position: fixed;
        top: 0;
        width: 100%;
    }
    .Grid
    {
        background-color: #fff;
        border: 1px solid #525252;
        border-collapse: collapse;
        color: #474747;
        float: left;
        font-family: Calibri;
        width: 99%;
    }