【问题标题】:How to intercept the event "Open link in new tab" on right-click to the link如何在右键单击链接时拦截“在新选项卡中打开链接”事件
【发布时间】:2013-10-03 21:01:43
【问题描述】:
 <a href="" ng-click="viewArticle($index +(currentPage*pageSize))">
    <img ng-src="{{suggestion.imageUrl}}" class="widget-cse__content__item-title-img"/>

    <div class="widget-cse__content__item-title">{{suggestion.title}}</div>
 </a>

我上面有这段代码。在点击事件中,viewArticle 方法被调用,点击计数器增加。由于此处未使用 href,因此它具有空值。但是,当用户右键单击链接并选择“在新选项卡中打开链接”时,我不知道如何处理。

【问题讨论】:

  • 你想右键点击触发 viewArticle 或者你想让它显示上下文菜单(其中包含“在新选项卡中打开链接”)?
  • 我希望右键单击的“在新选项卡中打开链接”的行为类似于 ng-click。谢谢。
  • 这个有运气吗?

标签: angularjs


【解决方案1】:

jQuery 方式

$('a').click(function(event){
    event.preventDefault();
    window.location = $(this).attr('href');
});

AngularJs 方式(伪代码)

添加一个禁用在新标签中打开的属性,并手动设置$location.path

element.click(function(event){
    event.preventDefault();
    $location.path = attrs.get('href');
});

【讨论】:

  • 我已经这样做了。我想以同样的方式处理 Chrome 中 contextMenu 的“在新选项卡中打开链接”(尚未在其他浏览器中测试过)。我的 viewArticle 代码: $scope.viewArticle = function (index) {SuggestService.postView($scope.suggestions[index].assetId).then(function (results) { $log.info(results); $scope.suggestions[索引].counters.views = results.viewCount; }); $window.open($scope.suggestions[index].url); }
【解决方案2】:

如果您想在点击事件上生成href,那么您应该使用ng-mousedown 事件,这样您就可以执行任何事件,例如open link in new tabopen link in new windowclick

HTML:

<a href="javascript:void(0)" ng-mousedown="viewArticle($event, data)">{{label}}</a>

JS:

  $scope.viewArticle = function (event, data) {

         // Do your work here
         .......

         // After that set href
         jQuery(event.target).attr('href', 'view/article');


  };

【讨论】:

    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    相关资源
    最近更新 更多