Javascript DOM元素存在时,jQuery可以';我不能操纵它
我有这个事件处理程序,我可以记录DOM元素,但我不能用它做任何事情。设置显示器或任何东西都没有任何效果Javascript DOM元素存在时,jQuery可以';我不能操纵它,javascript,jquery,css,meteor,Javascript,Jquery,Css,Meteor,我有这个事件处理程序,我可以记录DOM元素,但我不能用它做任何事情。设置显示器或任何东西都没有任何效果 Template.layoutTemplate.events({ 'mouseover .top_nav_button': function(ev){ ev.preventDefault(); console.log($(this).css('padding')); $(this).css('display', 'none'); } }); 以下是模
Template.layoutTemplate.events({
'mouseover .top_nav_button': function(ev){
ev.preventDefault();
console.log($(this).css('padding'));
$(this).css('display', 'none');
}
});
以下是模板代码:
<template name = 'layoutTemplate'>
<div id = 'top_nav'>
{{> loginButtons}}
<a class='top_nav_button' id = 'about_snipex_button' href="{{pathFor 'about'}}">About Snipex</a>
<a class='top_nav_button' id = 'become_verified_button' href="{{pathFor 'verified'}}">Become Verified!</a>
<a class='top_nav_button' id = 'terms_button' href="{{pathFor 'terms'}}">Terms&Conditions</a>
</div>
<h1 id='layout_header'><a id = 'home_button' href="{{pathFor 'home'}}">snippetExchange</a></h1>
<div id='layout_header_row_2'>
<h2 id='layout_by_line'><a id = 'home_button' href="{{pathFor 'home'}}">valuable answers</a></h2>
</div>
<div id='sub_nav'>
<a class='sub_nav_button' id = 'notifications_button' href="{{pathFor 'notifications'}}">notifications</a>
<a class='sub_nav_button' id = 'my_history_button' href="{{pathFor 'user_profile' _id=currentUser}}">my history</a>
<a class='sub_nav_button' id = 'new_post_button' href="{{pathFor 'new_post'}}">new post</a>
</div>
{{> yield}}
</template>
{{>loginButtons}
{{>产量}
作为隐藏的显示规则没有值
将其更改为$('#top_nav').css('display','none')代码>
隐藏是css中可见性规则的属性。使用隐藏()
事件侦听器接受两个参数:Event
和template
您可以通过执行以下操作找到要查找的元素:
'mouseover .top_nav_button': function(event, template){
event.preventDefault(); // likely not needed
template.$('#top_nav').hide();
// OR
template.$('#top_nav').css('display', 'none');
}
如果需要针对触发事件的元素,请使用:
'mouseover .top_nav_button': function(event, template){
event.preventDefault(); // likely not needed
var elem = event.target;
template.$(elem).hide();
}
这是我的黑客攻击…检查那里的代码样本现在我得到未捕获的类型错误:无法使用'in'运算符在未定义的
中搜索'padding'。。。该元素在my Stylesheet中有属性填充,您可能会收到该错误,因为$this元素不是您所期望的。试着做一个console.log($this)来确定你有什么。
'mouseover .top_nav_button': function(event, template){
event.preventDefault(); // likely not needed
var elem = event.target;
template.$(elem).hide();
}