gpt4 book ai didi

javascript - 使用 JQuery 在 HTML div 之后添加 CSS 类

转载 作者:行者123 更新时间:2023-11-30 08:49:56 24 4
gpt4 key购买 nike

我有这个 HTML

            <div class="portItem"></div>
<div class="portItem"></div>
<div class="portItem"></div>
<div class="portItem"></div>

这个 CSS

 .rectangle {
height: 250px;
width: 100%;
position: relative;
display: none;
background: #ffffff;
box-shadow: 1px 0px 20px black;
overflow-y: hidden;}

Here's the CSS for portItem, although not necessary (forms 4 green blocks next to each other)

.portItem {
height: 180px;
width: 250px;
position: relative;
display: inline-block;
background: #D0E182;
margin: 0 5px 5px 0;

我正在尝试在我点击的任何 portItem 之后添加 CSS 矩形。

这是我尝试使用的 JQuery 函数:

 $(document).ready(function() {
$('.portItem').click(function() {
addDiv($(this));
});
});



function addDiv($ele) {
var $box = $('<div />' '.rectangle');
$box.insertAfter($ele);
}

问题出在 var $box 语句上,我无法让它工作。

提前致谢!

最佳答案

您缺少一个逗号和一个对象。

var $box = $('<div />', {'class':'rectangle'});
// -------------------^ ^ ^
// added comma-----| |___________________|---and object literal syntax

class 两边的引号是支持旧版浏览器所必需的。您也可以改用 className

var $box = $('<div />', {className:'rectangle'});

关于javascript - 使用 JQuery 在 HTML div 之后添加 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18774262/

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