简体   繁体   English

将Wix HTML元素导入iFrame变量

[英]Getting Wix HTML element into iFrame variable

I have a wix webpage with an HTML element on the page. 我有一个wix网页,页面上带有HTML元素。 I need to get the text of that element into my iFrame that contains javascript. 我需要将该元素的文本放入包含JavaScript的iFrame中。 I'd like to store that text of the html element as a variable in my iFrame javascript. 我想将html元素的文本存储为iFrame javascript中的变量。

When I try the code below, my iFrame variable prints as 'undefined' in the web console. 当我尝试下面的代码时,我的iFrame变量在网络控制台中显示为“ undefined”。

<script>
var myElement = document.getElementById("#text15").text;
console.log(myElement);
</script>

The JavaScript function getElementById() receives as parameter the string of the id value it should search in the DOM . JavaScript函数getElementById()接收应该在DOM中搜索的id值的字符串作为参数。 So, you are searching for this: 因此,您正在搜索:

<div id="#text15"></div>

To find this element in the DOM: 要在DOM中找到此元素:

<div id="text15"></div>

You could either do: 您可以这样做:

var myElement = document.getElementById("text15").innerText;

Or if you like using the hash symbol when referencing elements from the DOM, you can also try: 或者,如果您喜欢在引用DOM中的元素时使用哈希符号,也可以尝试:

var myElement = document.querySelector("#text15").innerText;

Both work the same way. 两者的工作方式相同。 And also, use innerText which references as the content inside the tag. 并且,还可以使用innerText作为标签内的内容引用。 The text property of the DOM element returned by JavaScript does not exist. JavaScript返回的DOM元素的text属性不存在。

Note: You should not reference your DOM elements right in a <script> tag. 注意:您不应该在<script>标记中直接引用DOM元素。 Since most likely the elements won't be ready by the time you call them. 由于这些元素很可能在您调用它们时尚未准备就绪。

Try: 尝试:

<script>
window.onload = function() {
    var myElement = document.getElementById("#text15").innerText;
    console.log(myElement);
}
</script>

Look at an example of both ways: 看两个例子:

 var text1=document.querySelector("#myElement").innerText; console.log(text1); var text2=document.getElementById("myElement").innerText; console.log(text2); 
 <div id="myElement">Hello!</div> 

using jquery 使用jQuery

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#inPut').click(function() { var myElement = $('#text15').val(); console.log(myElement); }); }); </script> </head> <body> <br> <input type='text' id='text15'> <button id='inPut'>Write to console</button> <br> </div> </body> </html> 

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

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