如何将append上的div 再加上一些伪元素的类属性

这是append一个div的方法,

var descDiv = document.createElement('div');
document.getElementById("overlay").appendChild(descDiv);
var cssStr = "background-color: rgba(255,255,255,.8);z-index:1088; position:absolute;left:" + Left + "px; top:" + Top + "px;";
descDiv.style.cssText = cssStr;

注意: 这里的Top,Left值是变量。

这里是一个定义气泡的css, 最终效果见图形。
图片描述

有什么简单的办法给应用到刚才这个descDiv 上吗? 就可以随意的指定位置了

 .arrow_box {
    position: relative;
    border: 1px solid #c2e1f5;
    padding: 10px;
    width: 200px;
    height: 100px;
    border-radius: 6px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
    margin: 50px;
    float: left;
}

.arrow_box:before,
.arrow_box:after {
    position: absolute;
    display: block;
    width: 0;
    height: 0;
    border: solid transparent;
    pointer-events: none;
    content: "";
    border-color: rgba(136, 183, 213, 0);
}
.arrow_box.left:before {
    border-left-color: #c2e1f5;
    border-width: 13px;
    top: 38%;
    left: 100%;
}
.arrow_box.left:after {
    border-left-color: #fff;
    border-width: 10px;
    top: 41%;
    left: 100%;
}
阅读 3.1k
2 个回答

css里写好你要写的伪元素,直接descDiv.classList.add(),把 css类名添加到 descDiv 上。

楼上2位回答得非常正确,直接用classList.add() 可以直接加伪类的类名

 var helpinfoDiv = document.createElement('div');
    document.getElementById("overlay").appendChild(helpinfoDiv);
    var cssStr2 = "background-color: wsTextColorBlack;z-index:1088; box-shadow: 0 2px 1px rgba(0,0,0,.4); border:2px solid ; position:absolute;left:" + (Left + Width + 25) + "px;top:" + (Top + 0.5 * Height - 15) + "px; width:240px; height:30px;";
    helpinfoDiv.style.cssText = cssStr2;    
    // helpinfoDiv.innerHTML = "<<< Info of " + div_id;
    helpinfoDiv.innerHTML = " Enter Info ";
    helpinfoDiv.style.color = "white";
    helpinfoDiv.classList.add("helpinfoDiv");
    helpinfoDiv.classList.add("helpinfoDiv:before,helpinfoDiv:after");
    helpinfoDiv.classList.add("helpinfoDiv:before");
    helpinfoDiv.classList.add("helpinfoDiv:after");
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题