【问题标题】:Javascript inside javascriptjavascript里面的javascript
【发布时间】:2019-06-22 13:18:49
【问题描述】:

我正在使用一个 js 画廊(照片滑动),其中一张幻灯片不是图片,但有一些 html。

在那张幻灯片中是对使用onmousedown 提交的表单的事件跟踪,我无法开始工作。

所以我有:

some vars
items.push({ html: '<div and form start etc>
    <input type="submit" onmousedown="javascript:_paq.push(["trackEvent" etc ]);_paq.push(["etc"]);_paq.push(["etc"]);">
'});

我是通过摆弄和反复试验来学习的,对于新手问题,我很抱歉。

有什么想法吗?

【问题讨论】:

  • 尝试像[\"trackEvent\" etc ] 一样转义内部",如果失败,则移动内联onmousedown 代码并将事件附加到输入中
  • 转义不起作用,我会尝试附加到输入 - 你的意思是'oninput'?
  • 我已经尝试过 oninput 和 onsubmit 但没有成功 - 我会在问题中添加一些额外的信息
  • 在上面的 _paq.push 中添加以下代码可能会很有用:_paq.push(["trackEvent","Blog","Subscribe",location.pathname.substring(1) ]) 是不是 location.pathname.substring 造成了问题?
  • 你能展示你逃脱了什么吗?我说inner你刚刚是不是逃过了所有"

标签: javascript photoswipe


【解决方案1】:

以这种方式注入 JS 并不是最干净的事情。相反,您可以考虑使用事件委托的更全球化的方法:

在您页面的某处,包括一个点击监听器:

document.addEventListener('click', function (event) {
  if(event.target.classList.contains('js-track-subscribe-blog-location')) {
    _paq.push(["trackEvent","Blog","Subscribe",location.pathname.substring(1)])
  }
})

从该脚本运行的那一刻起,您页面上任何带有 js-track-subscribe-blog-location 类的内容都会在点击时触发您的跟踪代码。

只需在您的 &lt;input type="submit" 中包含该类,它应该可以工作。

【讨论】:

  • 你是明星级的唇膏!
  • 我已经添加了这个,它适用于注册事件和拉取 url 位置。原始代码中的 3 个 _paq.push 之一是用于推送在表单中输入的信息,但这不起作用,你看到什么了吗? _paq.push(['trackEvent','Blog','Subscribe',document.getElementById('form-option').value]);
  • 如果您的字段(我想是&lt;select&gt;)是页面上唯一带有id="form-option" 的内容,那么它应该可以工作。也就是说,如果你确实从这个表单中得到了东西,你应该考虑在表单级别工作:jsbin.com/nasocolusa/1/edit?html,js,console,output
猜你喜欢
  • 2018-11-10
  • 2011-05-06
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多