mootools获取li子元素的数据?
我正在尝试使用 mootools 获取 li
的子元素的一些信息,基本上我的 html 看起来像这样,
我希望能够使用 mootools 获取 a 标签的 id、class 和 href,到目前为止我的 javascript 看起来与此类似,
$$('.rate').each(function(element,i){
element.addEvent('click', function(){
var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar', 'sixstar', 'sevenstar', 'eightstar', 'ninestar', 'tenstar'];
myStyles.each(function(myStyle){
if(element.getParent().hasClass(myStyle)){
element.getParent().removeClass(myStyle)
}
});
myStyles.each(function(myStyle, index){
if(index == element.id){
element.getParent().toggleClass(myStyle);
var req = new Request({
method:'post',
url: '/recipes/save',
data: {'rating' : element.id},
onRequest: function(){ alert('Request made. Please wait...');},
onComplete:function(response){ alert('Response:' + response);}
}).send();
alert('Clicked '+element.id);
alert(element.getChildren().get('href');
}
});
});
});
脚本中的最后一个警报是我对 li(element) 的子元素及其 href 的尝试。
I am trying to get some information of a child element of an li
using mootools, essentially my html looks like this,
<li><a href="/home" id="home" class="nav-link">Home</a></li>
I am wanting to be able get the id, class and href of the a tag using mootools, so far my javascript looks similar to this,
$('.rate').each(function(element,i){
element.addEvent('click', function(){
var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar', 'sixstar', 'sevenstar', 'eightstar', 'ninestar', 'tenstar'];
myStyles.each(function(myStyle){
if(element.getParent().hasClass(myStyle)){
element.getParent().removeClass(myStyle)
}
});
myStyles.each(function(myStyle, index){
if(index == element.id){
element.getParent().toggleClass(myStyle);
var req = new Request({
method:'post',
url: '/recipes/save',
data: {'rating' : element.id},
onRequest: function(){ alert('Request made. Please wait...');},
onComplete:function(response){ alert('Response:' + response);}
}).send();
alert('Clicked '+element.id);
alert(element.getChildren().get('href');
}
});
});
});
The final alert in the script is my attempt to the child of the li(element) and its href.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的这个语句是不正确的
if(index == element.id){ ...
,index
始终是一个数字,你需要将其替换为myStyle
。证明: http://jsfiddle.net/fJDZJ/之后只需执行
el.get ('id')
、el.get('href')
和el.get('class')
;或者
el.getElement('a').get('id');
如果你需要获取子A的ID。This statement of yours is incorrect
if(index == element.id){ ...
,index
is always a number, you need to replace it withmyStyle
. Proof: http://jsfiddle.net/fJDZJ/After that just do a
el.get('id')
,el.get('href')
andel.get('class')
;Or
el.getElement('a').get('id');
if you need to get the ID of the child A.所有这些属性都以本机方式公开:
如果
element
包含对DOM 对象的引用,您可以获得子
和所有三个属性为:
或者正如 Oskar 所说,如果您不想担心跨浏览器问题,更好的选择是使用 Element/Elements 中的
get
方法来检索这些:All these properties are exposed natively as:
If
element
contains a reference to the<li>
DOM object, you can get the child<a>
and all three properties as:Or as Oskar says, if you don't want to worry about cross-browser issues, a better alternative is to use the
get
method in Element/Elements to retrieve these: