简体   繁体   English

Wix 自定义 javascript 仅在页面刷新时触发

[英]Wix custom javascript fires only on page refresh

Working with Wix webite, i added a custom code with some javascript to change some elements.使用 Wix 网站,我添加了一个带有一些 javascript 的自定义代码来更改一些元素。 Basically strip commas and transform words to links.基本上去除逗号并将单词转换为链接。

Code works fine in my simple test page.代码在我的简单测试页面中运行良好。 The wix website though, only fires the js by reloading the page.不过,wix 网站只会通过重新加载页面来触发 js。 I need it to fire on page first load.我需要它在页面首次加载时触发。

` `

window.onload = (event) => { 
 const mystring = document.querySelectorAll('#comp-id p span');
 for (const taglines of mystring) {
      const words = taglines.innerHTML.split(', ');
      for ( i = 0; i < words.length; i++) {
        var swords = words[i].replace(/\s+/g, '-').toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
        words[i] = `<a href="http://example.com/${swords}" class="menuintag">${words[i]}</a>`;
      }
      taglines.innerHTML = words.join(' ');
    }
};

` `

tested:测试:

 //window.onload = function() 
//  window.addEventListener("DOMContentLoaded", function()

i also tried directly in the built-in editor, mainpage.js我也直接在内置编辑器 mainpage.js 中尝试过

no success yet...还没有成功...

How can i do it please?请问我该怎么做?

Within a Wix site, you'll need to use Velo APIs to work with page elements instead of standard JavaScript functions.在 Wix 站点中,您需要使用 Velo API 来处理页面元素,而不是标准的 JavaScript 函数。

Take a look at their Hello World example to see how it works.查看他们的Hello World 示例,了解它是如何工作的。

See the Velo API Reference .请参阅Velo API 参考 You'll want to start with the Wix Editor Elements ($w) section.您需要从 Wix 编辑器元素 ($w) 部分开始。

I've never used Wix before.我以前从未使用过 Wix。

But Give this block a try.但是试试这个块。 There are a few different onLoad methods for better backwards compatibility有几种不同的onLoad方法可以更好地向后兼容

function onLoad() { 
 const mystring = document.querySelectorAll('#comp-id p span');
  for (const taglines of mystring) {
       const words = taglines.innerHTML.split(', ');
       for ( i = 0; i < words.length; i++) {
         var swords = words[i].replace(/\s+/g, '-').toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
         words[i] = `<a href="http://example.com/${swords}" class="menuintag">${words[i]}</a>`;
      }
      taglines.innerHTML = words.join(' ');
    }
}


if (window.attachEvent) { window.attachEvent('onload', onLoad); }
else if (window.addEventListener) { window.addEventListener('load', onLoad, false); }
else { document.addEventListener('load', onLoad, false); }

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

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