假设有一个使用Famo.us声明的面包含标记内容:
this.mySurface = new Surface({
size : this.options.targetSize,
content : '<a href="http://famo.us">This is a link</a>',
});是否有一种(容易)的方式来拦截点击事件?
奖励:除了拦截单击之外,如何传递单击事件,使默认处理程序也能完成其工作?
发布于 2014-08-13 22:58:00
有几种处理事件停止的方法。这里有一个解决方案,我认为在你的情况下效果最好。当然,以这种方式加载页面或链接有它自己的警告,我们不会深入到这里。
这里是完整的代码: jsFiddle上的实例
首先,我们跟踪目标表面中的点击,并调用函数来检查它是否是一个链接。如果我们单击一个链接,我们将发出一个传递目标href的事件。
this.mySurface.clickNullifier = function (e) {
if (e.target && e.target.nodeName == 'A' && e.target.href) {
this.mySurface.emit('href-clicked', { data: { href: e.target.href } })
return false;
}
}.bind(this);
this.mySurface.on('deploy', function () {
// sets up the click function on the surface DOM object
this._currentTarget.onclick = this.clickNullifier;
});现在,表面点击被跟踪,我们将捕获任何被截获的点击,并将其加载到iFrame中,或者如果本地链接使用著名的loadURL实用程序加载它们。
this.mySurface.on('href-clicked', function (event) {
console.log(event.data);
// handle your code for the iframe
// not always doable in the case of 'X-Frame-Options' to 'SAMEORIGIN'
loadIframe.call(this, event.data.href);
// or loadURL like here. Needs CORS open on the href server if cross origin
//Utility.loadURL(event.data.href, loadLink.bind(this));
}.bind(this));
function loadIframe(content) {
this.backSurface.setContent('<iframe src="' + content + '" frameborder="0" height="100%" width="100%"></iframe>');
};奖励:在上面的示例链接中,您将看到单击事件仍然在表面触发。您可以通过查看控制台日志来查看。
发布于 2014-08-12 10:28:30
不能在曲面中直接打开新链接,但可以添加类似于IFrameSurface:Famo.us IframeSurface的内容
正如aintnorest所暗示的那样,您很可能希望将事件处理程序添加到表面的单击中:
this.mySurface.on('click', function(e) {
// Bonus: on click do the standard redirect on main window
// or redirect of iFrame control
window.location = "http://famo.us";
});然后,而不是内容中的href,您只需要文本‘这是一个链接’,因为处理程序提供了通常期望的逻辑,但通过拦截。
如果出于某种原因需要将它们保留为“链接”,可以尝试向href添加散列,以避免通过默认行为进行实际导航。
作为另一种选择,因为我们正在使用javascript,所以您可以尝试这样的方法:
https://stackoverflow.com/questions/25252977
复制相似问题