作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的 View :
<a title="Post 1" id="car1" >
<article >
<figure >
<img src="img/car1.png" alt="image-1"
ng-click="getElementBeingPressed($event)">
<figcaption>Post 1</figcaption>
</figure>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate itaque consequatur illo quis, delectus, incidunt labore.</p>
</article>
</a>
我在 Controller 中有这个:
$scope.getElementBeingPressed = function(event)
{
// Get the button that opens the modal
var car = document.getElementById(event.target.id);
console.log('clicked id is: ' + event.target.id);
}
但问题是,当我按下图像时,我在控制台中得到了这个:
clicked id is:
Empty string passed to getElementById().
所以没有ID被传递。这是为什么?
如果有什么不清楚或需要额外解释,请评论出来,我会编辑问题。
谢谢你的时间
最佳答案
被按下的元素是 <img>
标签,因此为什么你的 ng-click
触发。
<img>
标签没有 ID。如果你想获得 <a>
使用该逻辑标记的 ID,移动 ng-click
在 <a>
上标签,因此:
<a title="Post 1" id="car1" ng-click="getElementBeingPressed($event)">
<article >
<figure >
<img src="img/car1.png" alt="image-1">
<figcaption>Post 1</figcaption>
</figure>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate itaque consequatur illo quis, delectus, incidunt labore.</p>
</article>
</a>
关于javascript - 按下 <a> 内的图像时如何传递 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50716916/
我是一名优秀的程序员,十分优秀!