0

我试图设计一个 js 算法,覆盖我的产品详细信息页面上的现有 HTML。这是在将 XML 集成到我的网站之后需要的,我想将此规范可视化为表格,但在 XML 集成之后,它只允许在我的网站上存储为 HTML 的段落标签。

所以;

这是我在 codepen 上运行良好但在我的 wordpress 网站上运行良好的代码。

JS

var a,b,c,d,e,f;
var bazes = document.getElementById("prd-desc").getElementsByTagName("div")[0].getElementsByTagName("p") ;
var bazes_t = document.getElementById("prd-desc").getElementsByTagName("div")[0];

a = bazes[0].innerText;
b = bazes[1].innerText;
c = bazes[2].innerText;
d = bazes[3].innerText;
e = bazes[4].innerText;
f = bazes[6].innerText;

//console.log(c);

bazes_t.innerHTML = '<div class ="outer-box"> <table id="urun_inf"> <tr> <td class="col1-head"> Kod </td> <td class="col2-desc">a</td> </tr> <tr> <td class="col1-head"> Ebat </td> <td class="col2-desc"> Açıklama2 </td> </tr> <tr> <td class="col1-head"> Reklam Alanı</td> <td class="col2-desc"> Açıklama3 </td> </tr> <tr> <td class="col1-head"> Renk </td> <td class="col2-desc"> Açıklama4 </td> </tr> <tr> <td class="col1-head"> Merkez Stok </td> <td class="col2-desc"> Açıklama5 </td> </tr> <tr> <td class="col1-head"> Toplam Stok </td> <td class="col2-desc"> Açıklama6 </td> </tr> </table> </div><style> .outer-box{ border : 2px solid; border-style : groove; width : %100; height : auto;}.col1-head,.col2-head{ font-weight : bold; color : blue;}.col1-desc::before,.col2-desc::before{ content : ":";}</style>';

console.log(bazes_t.getElementsByTagName("td")[1].innerText);

bazes_t.getElementsByTagName("td")[1].innerText = a;
bazes_t.getElementsByTagName("td")[3].innerText = b;
bazes_t.getElementsByTagName("td")[5].innerText = c;
bazes_t.getElementsByTagName("td")[7].innerText = d;
bazes_t.getElementsByTagName("td")[9].innerText = e;
bazes_t.getElementsByTagName("td")[11].innerText =f;
<div data-id="6f6ba5e" class="elementor-element elementor-element-6f6ba5e elementor-widget elementor-widget-product-description" id="prd-desc" data-element_type="product-description.default">
				<div class="elementor-widget-container">
			<p>Lacivert</p>
<p>Bisiklet Yaka Lacivert Tişört</p>
<p>M Beden</p>
<p>1458</p>
<p>1458</p>
<p>&nbsp;</p>
<p>* %100 Pamuklu * 30/1 Süprem Kumaş * İstenilen renk ve bedenlerde dikim yapılır * Yandan Dikişli * 145 gr.</p>
		</div>
				</div>

所以这段代码在 codepen 或这里看起来很棒。我在我的 OceanWP wordpress 网站上通过 Elementor 将这段代码实现为“Woocommerce 的单一产品页面”,我可以使用插件重新设计这个页面。(它允许我向该页面添加元素或自定义 HTML,并在“脚本/脚本”标签中实现此代码)

每个产品都有独特的规格,因此这个 JS 必须为每个产品页面获取数据并像上面的代码一样将其显示为表格。

提前谢谢了,

C。

4

1 回答 1

1

不知道你想用这段代码做什么,只需改变你的 javascript 代码(只需要添加 window.onload 就可以了。

window.onload = function(){
  var a,b,c,d,e,f;
  var bazes = document.getElementById("prd-desc").getElementsByTagName("div")[0].getElementsByTagName("p");
  var bazes_t = document.getElementById("prd-desc").getElementsByTagName("div")[0];

  a = bazes[0].innerText;
  b = bazes[1].innerText;
  c = bazes[2].innerText;
  d = bazes[3].innerText;
  e = bazes[4].innerText;
  f = bazes[6].innerText;

  //console.log(c);

  bazes_t.innerHTML = '<div class ="outer-box"> <table id="urun_inf"> <tr> <td class="col1-head"> Kod </td> <td class="col2-desc">a</td> </tr> <tr> <td class="col1-head"> Ebat </td> <td class="col2-desc"> Açıklama2 </td> </tr> <tr> <td class="col1-head"> Reklam Alanı</td> <td class="col2-desc"> Açıklama3 </td> </tr> <tr> <td class="col1-head"> Renk </td> <td class="col2-desc"> Açıklama4 </td> </tr> <tr> <td class="col1-head"> Merkez Stok </td> <td class="col2-desc"> Açıklama5 </td> </tr> <tr> <td class="col1-head"> Toplam Stok </td> <td class="col2-desc"> Açıklama6 </td> </tr> </table> </div><style> .outer-box{ border : 2px solid; border-style : groove; width : %100; height : auto;}.col1-head,.col2-head{ font-weight : bold; color : blue;}.col1-desc::before,.col2-desc::before{ content : ":";}</style>';

  console.log(bazes_t.getElementsByTagName("td")[1].innerText);

  bazes_t.getElementsByTagName("td")[1].innerText = a;
  bazes_t.getElementsByTagName("td")[3].innerText = b;
  bazes_t.getElementsByTagName("td")[5].innerText = c;
  bazes_t.getElementsByTagName("td")[7].innerText = d;
  bazes_t.getElementsByTagName("td")[9].innerText = e;
  bazes_t.getElementsByTagName("td")[11].innerText =f;
}
于 2018-09-09T22:29:31.410 回答