简体   繁体   English

在 div 中更改标题 iframe(没有 id)

[英]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.我试图弄清楚如何使用 javascript 向没有“id”的 iframe 添加标题。

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.我是 jquery 的菜鸟,所以我不知道这是否正确。

Try removing the .尝试删除. from .review-right class in the HTML and in the Script just use basic DOM来自 HTML 中的.review-right类,在脚本中只使用基本的 DOM

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

JQuery Solution jQuery 解决方案

 $(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>

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

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