Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用绝对位置来定位TD的子级_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 使用绝对位置来定位TD的子级

Javascript 使用绝对位置来定位TD的子级,javascript,jquery,html,css,Javascript,Jquery,Html,Css,嗨,我正试着将一个div放置在一个TD内部div的左上角,灵感来自于此 这是为了帮助定位TD中的元素。我不能让它工作。我想可能是因为我有填充物。如果你能帮我把代码中的“target”元素放到TD的左上角,那就太好了 我试着得到TD内部包装的宽度和高度,看起来高度就是文本。我想我希望内包装的高度与TD相同,这样可以将目标div定位在角落中。我听说你不能在TDs中定位 <!doctype html> <html lang="en"> <head> <

嗨,我正试着将一个div放置在一个TD内部div的左上角,灵感来自于此 这是为了帮助定位TD中的元素。我不能让它工作。我想可能是因为我有填充物。如果你能帮我把代码中的“target”元素放到TD的左上角,那就太好了

我试着得到TD内部包装的宽度和高度,看起来高度就是文本。我想我希望内包装的高度与TD相同,这样可以将目标div定位在角落中。我听说你不能在TDs中定位

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
        td,th{
            padding: 20px 30px;
        }
        .innerWrapper{
            position:relative;
        }
        .target{
            position: absolute;
            top:0;
            left: 0;
            background-color: blue;
        }
        td:nth-child(1){
            background-color: yellow;
        }
    </style>
    <script  src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
    <table>
        <tr>
            <th>one</th>
            <th>two</th>
            <th>three</th>
            <th>four</th>
        </tr>
        <tr>
            <td><div class = "innerWrapper">td1<div class = "target">T</div></div></td>
            <td>td2</td>
            <td>td3</td>
            <td>td4</td>
        </tr>
    </table>
        <script>
            $(document).ready(function(){
                tdh = $('td:nth-child(1)').outerHeight()
                alert("td"+ tdh);
                var inner = $('.innerWrapper').outerHeight(tdh);
                alert(inner);
            });
        </script>
</body>
</html>

桌子
td,th{
填充:20px 30px;
}
.innerWrapper{
位置:相对位置;
}
.目标{
位置:绝对位置;
排名:0;
左:0;
背景颜色:蓝色;
}
运输署:第n名儿童(1){
背景颜色:黄色;
}
一
二
三
四
td1T
td2
td3
td4
$(文档).ready(函数(){
tdh=$('td:n个子项(1)')。outerHeight()
警报(“td”+tdh);
var-inner=$('.innerWrapper').outerHeight(tdh);
警报(内部);
});

我试图制作一个脚本,将innerwrapper设置为与TD相同的高度,但没有成功,我想知道为什么。如果您也能帮忙的话,那就太好了。

您需要将填充物从单元格移动到包装中(或者适当地偏移目标零件)。

谢谢。如果我从td移动填充,那么在其他单元格中保留填充的最佳方法是什么td:not(td:nth child(1)){padding:20px 30px;}不起作用td,th{padding:20px 30px;}td:nth child(1){padding:0px;}似乎起作用