简体   繁体   中英

Change title iframe (that has no id) within a div

I'm trying to figure out how to add a title to a iframe that has no 'id' using javascript.

The code is like this:

<div class="review-right">
<div>
<iframe></iframe>
</div>
</div>

And I want to get this:

<div class="review-right">
<div>
<iframe title="Sample"></iframe>
</div>
</div>

This is what I've tried:

<script type="text/javascript">
$(document).ready(function(){
   $(".review-right iframe").attr('title', 'Sample');
});
</script>

I'm a noob at jquery so I don't know if this is even right.

Try removing the . from .review-right class in the HTML and in the Script just use basic DOM

 document.querySelector(".review-right div iframe").title = 'Sample Vanilla'
 <div class="review-right"> <div> <iframe></iframe> </div> </div>

JQuery Solution

 $(document).ready(function(){ $('.review-right iframe').prop('title', 'Sample Jquery'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="review-right"> <div> <iframe></iframe> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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