gpt4 book ai didi

javascript - overflow-x 在 Jquery 中动态生成的 DIV 中不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 23:45:12 24 4
gpt4 key购买 nike

大家好,我是 jquery 的新手,我有两个疑问要问我的编码......

这是我的 XHTML 代码以及我的 JQUERY 和 CSS

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Test</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<meta name="viewport" content="width=device-width"/>
<style>
#filterpanel{
position: absolute;
top: 50%;
left: 0;
width: 99%;
height: 20%;
background-color: azure;
border: 4px solid black;
border-radius: 12px;
overflow-x: scroll;
}
</style>
</h:head>
<h:body>
<div id="filterpanel"></div>
<script>
$(document).ready(function(){

for (var i = 0; i &lt; 6; i++) {

var s=$('<div id="filterinfo'+i+'" ><div id="imgclose'+i+'" style="position: absolute; right:0;top: 0;cursor: pointer;" ><img src="images/close.png" width="20px" height="20px"/></div></div>').css({"text-align":"center","background-color":"black","color":"white","position":"relative","width":"16%","height":"98%", "border-radius":"16px","top":"1%","float":"left","left":"0.5%","margin-left":"1%"});
s.appendTo("#filterpanel");
$("imgclose"+i).click(function(){
alert("clicked");
$("filterinfo"+i).hide();
});
}

});
</script>
</h:body>
</html>

问题是我的所有 div 都按照我的要求使用特定的 id 成功生成,但是如果我的值超过 5,则生成的 div(即)filterinfo6 显示在 filterinfo1 下面而不是 filterinfo5 的右侧我的主 div 过滤器面板,尽管我已经将 overflow-x 作为主 div 的滚动条。

我的第二个疑问是,当我点击 imgclose div 时,点击功能不起作用......我真的需要关闭特定的 div(即)如果用户点击 imgclose4,那么应该删除特定的 div filterinfo4从我的主 div 过滤器面板....到目前为止,我尝试过的方法没有用....有人可以帮我解决这个问题吗。

这是一个演示:http://jsfiddle.net/3XwpG/

提前致谢。

最佳答案

第一件事是将 filterinfo 的 css 更改为:

{
"text-align": "center",
"background-color": "black",
"color": "white",
"display": "inline-block",
"width": "16%",
"height": "98%",
"border-radius": "16px",
"margin-left": "1%"
}

第二件事是您必须定义 #filterpanel 的高度并制作 white-space: nowrap

#filterpanel{
position: absolute;
top: 50%;
left: 0;
width: 99%;
height: 100px;
position: relative;
background-color: azure;
border: 4px solid black;
border-radius: 12px;
overflow-x: scroll;
white-space: nowrap;
}

DEMO

关于javascript - overflow-x 在 Jquery 中动态生成的 DIV 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21725584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com