简体   繁体   English

如何使用 jQuery 将 div id 添加到 div?

[英]How to prepend div id to div using jQuery?

How can I add div id of a selected division and prepend it to the division itself?如何添加所选部门的 div id 并将其添加到部门本身?

Here's what I tried:这是我尝试过的:

 $(".showID").prepend("<h4>" + $(this).attr("id") + "</h4>"); console.log( $(".trialID").attr("id") ); /*Why does this work?*/
 div{ background:royalblue; color:#ddd; padding:2rem;margin-bottom:1rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="foobar" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="loremIpsum" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="helloWorld" class="noID"> Hello <p>This is a sample paragraph.</p> </div> <div id="JonSkeet" class="showID trialID"> Hello <p>This is a sample paragraph.</p> </div>

When I use $(".trialID").attr("id");当我使用$(".trialID").attr("id"); in browser console it does work...在浏览器控制台中它确实有效......

Is there a way to achieve this with CSS??有没有办法通过 CSS 实现这一目标?

You have to iterate over all selected $('.showID') elements with jQuery.each() :您必须使用jQuery.each()遍历所有选定$('.showID')元素:

 $.each($('.showID'), function () { $(this).prepend($('<h4>').text($(this).attr('id'))) })
 div{background:royalblue; color:#ddd;padding:2rem;margin-bottom:1rem;}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="foobar" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="loremIpsum" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="helloWorld" class="noID"> Hello <p>This is a sample paragraph.</p> </div> <div id="JonSkeet" class="showID trialID"> Hello <p>This is a sample paragraph.</p> </div>

As I understand, you can do this task using jQuery, but not sure whether you want to do it entirely using CSS.据我了解,您可以使用 jQuery 完成此任务,但不确定是否要完全使用 CSS 完成此任务。

The problem with your code is that $(this) doesn't refer to the current element.您的代码的问题是$(this)没有引用当前元素。 You need to loop through the selected div s and manually prepend the IDs to those.您需要遍历选定的div并手动将 ID 添加到那些前面。 You can refer to the current element using $(this) then.然后,您可以使用$(this)引用当前元素。

 $(".showID").each(function() { $(this).prepend(`<h4>${$(this).attr("id")}</h4>`); });
 div { background: royalblue; color: #ddd; padding: 2rem; margin-bottom: 1rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="foobar" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="loremIpsum" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="helloWorld" class="noID"> Hello <p>This is a sample paragraph.</p> </div> <div id="JonSkeet" class="showID trialID"> Hello <p>This is a sample paragraph.</p> </div>

Jquery issue Jquery 问题

So your jquery needs to loop through the class .showID or .trialID所以你的 jquery 需要循环通过 class .showID.trialID

 $(".showID").each(function(){ $(this).prepend("<h4>" + $(this).attr("id") + "</h4>"); });
 div{ background:royalblue; color:#ddd; padding:2rem;margin-bottom:1rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="foobar" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="loremIpsum" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="helloWorld" class="noID"> Hello <p>This is a sample paragraph.</p> </div> <div id="JonSkeet" class="showID trialID"> Hello <p>This is a sample paragraph.</p> </div>

CSS issue CSS 问题

Answering the css, there's no way to dynamically get the id attribute.回答css,没有办法动态获取id属性。 But you can do something like this where it can get the id that starts with specific characters:但是你可以做这样的事情,它可以获得以特定字符开头的 id:

div[id^="id_"].showID { color: red; }

You can add the heading to the divs using JS/jQuery and then decide to show it or not using CSS您可以使用 JS/jQuery 将标题添加到 div,然后使用 CSS 决定是否显示它

See below:见下文:

 $(".showID").each(function(){ $(this).prepend("<h4>" + $(this).attr("id") + "</h4>"); });
 div{ background:royalblue; color:#ddd; padding:2rem;margin-bottom:1rem; } div[id^="id_"].showID > h1{ display:block } div[id^="no_"].showID > h1{ display:none }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="id_foobar" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="id_loremIpsum" class="showID"> Hello <p>This is a sample paragraph.</p> </div> <div id="no_helloWorld" class="noID"> Hello <p>This is a sample paragraph.</p> </div> <div id="id_JonSkeet" class="showID trialID"> Hello <p>This is a sample paragraph.</p> </div>

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

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