简体   繁体   English

如何在 iframe 中创建 HTML5 数据属性

[英]How to create HTML5 data attribute in an iframe

I'm trying to pull the innerText of an H1 (which isn't in the iframe and has the "product name" in it) into the of a form button that is in an owned iframe (not-3rd party).我正在尝试将 H1(不在 iframe 中并且其中包含“产品名称”)的 innerText 拉入拥有的 iframe(非第 3 方)中的表单按钮。 This is my code below, which obviously isn't working.这是我下面的代码,显然不起作用。 Thank you in advance!先感谢您!

<script type='text/javascript'>

var productName = document.getElementsByTagName("h1")[0].getAttribute("innerHTML");
var links = document.querySelectorAll("input");
for(var i = 0; i < links.length; i++) {
  links[i].dataset.productName = productName;
}

</script>

Instead of getAttribute("innerHTML") do document.getElementsByTagName("h1")[0].innerHTML .而不是getAttribute("innerHTML")document.getElementsByTagName("h1")[0].innerHTML That will get you the actual product name.这将为您提供实际的产品名称。

window.frameElement.getAttribute('data');

Try this尝试这个

<script type='text/javascript'>

var productName = document.getElementsByTagName("h1")[0].innerText;
var links = window.frames['<your iframe name>'].document.querySelectorAll("input");
for(var i = 0; i < links.length; i++) {
  links[i].dataset.productName = productName;
}

</script>

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

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