0

大家好,我对 Angularjs 很陌生,我一直在尝试将一个简单的 jQuery 脚本转换为 Angular,但似乎几乎不可能。我需要你的帮助,请在下面找到我的代码

---- HTML CODE ----
<iframe name="web" width="600px" height="400px"></iframe>
<a href="http://www.lipsum.com/">Lorem Ipsum</a>

还有我的 jQuery

---- JQUERY ----
$(document).ready(function(){
  $('a').attr('target', 'web');
});

提前致谢。

4

3 回答 3

4

只需使用target已设置的属性声明您的锚点:

<a href="http://www.lipsum.com/" target="web">Lorem Ipsum</a>

如果您在事后添加此属性(大概是因为您想绕过任何抱怨的验证噪音target),您可以使用指令来执行此操作:

标记

<a href="http://www.lipsum.com/" my-target="web">Lorem Ipsum</a>

指示

angular.module('whatever', []).directive('myTarget', function() {
    return {
        link: function( scope, el, attrs ) {
            el.attr('target', attrs.myTarget);
        }
    };
});

DEMO

于 2013-08-15T11:16:41.850 回答
2
---- HTML ----
<div ng-controller="MainCtrl">
   <a href="http://www.lipsum.com/" target="web">Lorem Ipsum</a>
</div>
于 2013-08-15T11:17:27.883 回答
2

我不知道 angularjs 处理目标的方式是什么,但设置它的普通 JavaScript 方式是

 var i,a=document.getElementsByTagName('a'),n=a.length;
 for (i=0;i<n;i++) a[i].setAttribute('target','web')

(当然,一旦 DOM 很容易加载,这需要在触发的部分中完成。)

于 2013-08-15T11:47:53.710 回答