【发布时间】:2015-09-13 19:30:27
【问题描述】:
window.location.href 有一些问题。 在我的网站中,我使用 AngularJS(框架)。 我得到了一张包含以下代码的表格:
<table id="MAND">
<tbody id="hoveren">
<tr ng-repeat="artist in artists | filter:name">
<td class="user-name"><img id="{{artist.div}}" style="vertical-align: middle;" src="{{artist.img}}"></td>
<td class="user-email" style="vertical-align: middle;">{{artist.name}}</td>
<td class="user-phone" style="vertical-align: middle;">{{artist.description}}</td>
<td class="user-phone" style="vertical-align: middle;">{{artist.country}}</td>
</tr>
</tbody>
</table>
所以你看到给图像一个 divname。
然后在jQuery中,我调用如下函数:
$("#crunch").click(function() {
window.location.href = "http://example.com/new_url";
});
在这种情况下,{{"artist.div"}} 等于 crunch,这就是 #crunch 的原因。
为什么这不起作用?
我点击它但没有任何反应。
这是某种愚蠢的错误吗?
谢谢!
顺便说一句,如果你想知道,我的 angularjs 部分:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
angular.module('App', [])
.controller('Controller', function($scope){
$scope.artists = [
{
"name": "Crunchyroll",
"country": "All countries except Japan",
"img":"images/crunchy.png",
"description":"You can set it to everything you want!",
"div":"crunch"
},
{
"name": "Rhapsody",
"country": "US only, be at the right spot",
"img":"images/rhap.png",
"description":"You can set it to everything you want!"
},
{
"name": "Febreze",
"country": "US only",
"img":"images/feb.jpg",
"description":"You can set it to everything you want!"
},
{
"name": "Kellogs Froot Loops",
"country": "US only",
"img":"images/kel.jpg",
"description":"You can set it to everything you want!"
},
{
"name": "Pure Asia Garcinia Weight Loss",
"country": "US, AU, CA, UK and NZ only",
"img":"images/bottle.png",
"description":"You can set it to everything you want!"
},
{
"name": "Free Computer",
"img":"images/pc.png",
"description":"You can set it to everything you want!"
},
{
"name": "whateveee",
"country": "All countries except Japan",
"img":"images/crunchy.png",
"description":"You can set it to everything you want!",
"div":"crunch"
}
];
});
顺便说一句,不知道怎么放。
谢谢!
【问题讨论】:
-
您的 jQuery 代码可能在这些 div 分配该 ID 之前运行,因此事件侦听器无法附加到不存在的元素。我强烈建议您改用 ng-click。
标签: javascript jquery html css angularjs