簡體   English   中英

我如何使用 javascript 提取 html 內容?

[英]How do i pull in html content using javascript?

我想使用 javascript 來提取 html 代碼。 我已經像這樣設置了 html 代碼;

<html>
<div id="tesingCode">
    <h1>Title</h1>
    <p>testOfCodetestOfCodetestOfCodetestOfCode</p>
</div>
</html>

然后我創建了一個這樣的變量;

var testingTheCode = document.getElementById ("tesingCode");

並嘗試將變量添加到 Polymer 項目中;

Polymer({

is: 'animated-grid',

behaviors: [
  Polymer.NeonSharedElementAnimatableBehavior
],

properties: {

  config: {
    type: Array,
    value: function() {
      return [
        {value: 1, color: 'blue'},
        {value: 2, color: 'red'},
        {value: 3, color: 'blue'},
          {testingTheCode, color: 'green'},
        {value: 5, color: 'yellow'},
        {value: 6, color: 'blue'},
        {value: 7, color: 'red'},
        {value: 8, color: 'green'},
        {value: 9, color: 'yellow'},
        {value: 10, color: 'red'}
      ]
    }
  },

完整的文件是 animation-grid.html 可以在這里找到...

https://github.com/alistairkb/Coachella/blob/master/animated-grid.html

我的總體目標是在按下之前將內容添加到框中。

你很接近,我想你正在尋找innerHTML屬性

看看這個JSfiddle

您只需要將 .innerHTML 放在 testingcode 變量的末尾

var testingTheCode = document.getElementById ("tesingCode").innerHTML;

如果這不是您要查找的內容,請發布您的代碼的 jsFiddle。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM