简体   繁体   English

如何在 jQuery 动态创建的元素上添加 ::before 伪元素

[英]How to add ::before pseudo element on elements created dynamically by jQuery

I am creating some elements dynamically with jquery. (say with id test_element1 , test_element2 and so on..)我正在使用 jquery 动态创建一些元素。(比如说 id test_element1test_element2等等......)

I have the below CSS -我有以下 CSS -

div[id^=test_]:before  {
    content: "";
    height: 100%;
    width: 100%;
    box-shadow: #aaaaaa 0px 0px 10px inset;
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: -1;
}

The ::before element does not show up when I inspect the element.当我检查元素时, ::before元素没有显示。 It shows up only if the test_element1 is already present in my HTML (ie. static content).仅当test_element1已存在于我的 HTML(即 static 内容)中时,它才会显示。

How do I make the ::before appear for my dynamic elements?如何让::before出现在我的动态元素中?

There really should be no reason for the pseudo element not showing up.伪元素真的没有理由不出现。 Maybe check your code to see if there is a problem elsewhere.也许检查您的代码以查看其他地方是否有问题。 Here is a working example of what you are trying to achieve, maybe you can compare this with your code and find a solution for your specific case.这是您尝试实现的工作示例,也许您可​​以将其与您的代码进行比较,并为您的特定情况找到解决方案。 But the ultimate answer is that it should be working, so the problem is not the css/jquery relationship.但最终的答案是它应该可以工作,所以问题不在于 css/jquery 关系。

 $("body").append( $("<div />", { "id": "test_element1" }).html("test"), $("<div />", { "id": "test_element2" }).html("test"), $("<div />", { "id": "test_element3" }).html("test") );
 div[id^=test_] { position: relative; padding-top: 1em; } div[id^=test_]::before { content: "i am pseudo element"; height: 100%; width: 100%; box-shadow: #aaaaaa 0px 0px 10px inset; position: absolute; left: 0px; top: 0px; z-index: -1; display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

div[id^=test_] {
    position: relative;
}
div[id^=test_]:before  {
    content: "";
    height: 100%;
    width: 100%;
    box-shadow: #aaaaaa 0px 0px 10px inset;
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: -1;
}

This will be the solution to your question.这将是您问题的解决方案。

 var count = 0; var interval = setInterval(function(){ if(count < 5){ let btn = document.createElement('button'); let len = document.querySelectorAll('button').length; btn.classList.add('text_'+ parseInt(len + 1)); btn.innerText = 'Button ' + parseInt(len + 1); document.body.appendChild(btn); count+=1; }else{ clearInterval(interval) } }, 5000)
 button{ position:relative; display:block; padding:10px; background: dodgerblue; color:#fff; margin-bottom:10px; border: none; border-radius:5px; } button[class^="text_"]:before{ content: 'Before'; position:absolute; left: 100%; height:30px; padding: 0 10px; border-radius: 50%; color: #000; }
 <button class="text_1">Button 1</button>

Please check this code snippet and fix your code accordingly.请检查此代码段并相应地修复您的代码。 Here I am generating dynamic button with dynamic id.在这里,我正在生成带有动态 id 的动态按钮。 I have also specified css property using pseudo attribute selector.我还使用伪属性选择器指定了 css 属性。

Edit your code and let me know your thoughts.编辑您的代码,让我知道您的想法。 I hope this will fix your problem.我希望这会解决你的问题。

When you set pseudo element, selector z-index + position need to added.设置伪元素时,需要添加选择器 z-index + position。

div[id^=test_]  {
    position:relative;
    z-index:1;
}
div[id^=test_]:before  {
    content: "";
    height: 100%;
    width: 100%;
    box-shadow: #aaaaaa 0px 0px 10px inset;
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: -1;
}

the answer is that答案是

You can not manipulate the pseudo elements like :after and :before using javascript or jquery.您不能使用 javascript 或 jquery 操作像 :after 和 :before 这样的伪元素。 You cannot even add then dynamically.你甚至不能动态添加 then 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM