为什么只有在我控制台记录了错误的变量时才执行此附加操作 [英] Why does this append only work if I console log a bad variable

查看:59
本文介绍了为什么只有在我控制台记录了错误的变量时才执行此附加操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jquery还是比较陌生,并且试图在每次单击时更改js手风琴上的向上和向下箭头,不幸的是,我遇到了一个错误,该错误仅在console.log一个错误的变量时才起作用.是否有人对我onclick ="embiggen(1)"时我可能做错的事情有任何指导,例如,如果它的手风琴为id?

I am relatively new with jquery, and am trying to change an up and down arrow on a js accordion on each click, unfortunately, I have run into an error where it only works if I console.log a bad variable. Does anyone have any guidance as to what I might be doing wrong when I onclick="embiggen(1)" for example if its accordion id one?

function arrowup(id){
$('#downarrow'+id).remove();
$('#dropdown'+id).append('</a>');
$('#dropdown'+id).append('<i id="uparrow'+ id +'" class="icon-1 icon-chevron-up">');
}

function arrowdown(id){
$('#uparrow'+id).remove();
$('#dropdown'+id).append('</a>');
$('#dropdown'+id).append('<i id="downarrow'+ id +'" class="icon-1 icon-chevron-down">');
}


//Switches the arrows
function embiggen(id){

var up = $('#uparrow'+id).length;
if (up == 1){
    arrowdown(id);
    console.log(i see you);
}
var down = $('#downarrow'+id).length;
if (down == 1){
    arrowup(id);
}

}

推荐答案

对于每个注释,以下是使用伪元素的方法.我使用CSS箭头,但是您可以给它们固定的宽度和高度,并使用背景图像作为图标.

Per comments, here's how to do it with pseudo elements. I'm using CSS arrows but you could just give them a fixed width and height and use a background image as icon.

演示: http://jsbin.com/oyemaj/1/edit

HTML:

<div class="accordion">
  <h2>Lorem ipsum</h2>
  <p>Porta est magna adipiscing...</p>
</div>

CSS:

.accordion h2 {
  position: relative;
  padding: .5em;  
  background: #ddd;
  cursor: pointer;
}

.accordion h2:after {
  content: "";
  position: absolute;
  top: 50%;
  right: 10px;
  margin-top: -5px;
  border: 10px solid transparent;
  border-top-color: blue;
}

.accordion h2.open:after {
  margin-top: -15px;
  border-color: transparent;
  border-bottom-color: blue;
}

.accordion p { display: none }

jQuery:

$('.accordion h2').click(function() {
  $(this).toggleClass('open').next().slideToggle();
});

是的,如果使用这种方法,那就是您需要的所有jQuery.

Yup, that's all the jQuery you need if you use this approach.

这篇关于为什么只有在我控制台记录了错误的变量时才执行此附加操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆