<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
标签 javascript php jquery html css
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
在我的 PHP 元素中,我有一些评论框 div 会 self 重复,但是每次我想要有人点击它(div 本身),将 div 的宽度更改为 100%,问题是来到这里,每当我尝试做这样的事情时,它都会改变所有 div 的宽度,而不是单独改变它自己的特定框,你的脑海中有什么解决方案吗?
最佳答案
只需为您的 div
设置一个 ID
(如果您只想修改整个网页中的一个元素)并从 JQuery
(如您所标记的那样)。
如果你想修改你点击的所有div
(但每次只会修改你点击的div
),可以引用class
关联到每个 div。
$( document ).ready(function() {
$(".div").click(function() {
$(this).css("width", "100%");
});
});
html,body{
width: 100%;
height: 100%;
}
.div{
height: 100px;
width: 100px;
background-color: red;
margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="id1" class="div"></div>
<div id="id2" class="div"></div>
编辑:如果您希望它们回到初始位置,只需在您的 Javascript
中执行一个 if
语句即可。
$( document ).ready(function() {
$(".div").click(function() {
if($(this).css("width") == "100px")
$(this).css("width", "100%");
else
$(this).css("width", "100px");
});
});
html,body{
width: 100%;
height: 100%;
}
.div{
height: 100px;
width: 100px;
background-color: red;
margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="id1" class="div"></div>
<div id="id2" class="div"></div>
关于javascript - 尝试更改在 php 上重复的特定 div 宽度失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37440721/
相关文章:
jquery - Ajax:使用选择框将页面内容加载到div
javascript - 如何实现自动滚动以跟随我的 div
javascript - JavaScript 的广播 channel 是否限制为每秒接收一条消息?
javascript - 从 dom 到 jquery 的函数
php - 非静态方法不应该静态调用,假设 $this 来自不兼容的上下文 -Laravel 5.2
php - 将 unicode 字符写入 php 中的文件 - 不重复
jquery - 如何将文件内容从 HTML5 FileUpload 传递到 Web 服务(asmx)?
javascript - knockout 选择选项和依赖项
javascript - 使用 jQuery,通过 id 选择一个 Div,从下拉列表中选择并将其绘制在另一个 Div 中