Javascript 里面 javascript
Javascript inside javascript
我正在使用 js 画廊(photoswipe),其中一张幻灯片不是图片,但有一些 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"]);">
'});
我是在摆弄和试错中学习的,很抱歉新手问题。
有什么想法吗?
以这种方式注入 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)])
}
})
从该脚本具有 运行 的那一刻起,您页面上带有 class js-track-subscribe-blog-location
的任何内容都会在点击时触发您的跟踪代码。
只需将 class 添加到您的 <input type="submit"
中,它应该可以正常工作。
我正在使用 js 画廊(photoswipe),其中一张幻灯片不是图片,但有一些 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"]);">
'});
我是在摆弄和试错中学习的,很抱歉新手问题。
有什么想法吗?
以这种方式注入 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)])
}
})
从该脚本具有 运行 的那一刻起,您页面上带有 class js-track-subscribe-blog-location
的任何内容都会在点击时触发您的跟踪代码。
只需将 class 添加到您的 <input type="submit"
中,它应该可以正常工作。