简体   繁体   English

Javascript功能无法按功能使用

[英]Javascript function not working per functionality

I have a following Javascript function for expand collapse of the content: 我具有以下用于扩展内容折叠的Javascript函数:

function showAns(inp){

        var hide="#hideAns"+inp;
        var show="#showAns"+inp; 
        var ansdiv ="#ans"+inp;
      $(hide).click(function(){
            $(ansdiv).hide(500);
            $(hide).hide();
            $(show).show();
        });
          $(show).click(function(){
            $(ansdiv).show(500);
            $(hide).show();
            $(show).hide();
        });
    }

This function is called from following piece of generated HTML code: 从以下生成的HTML代码中调用此函数:

 <div class="qbox">
        <span class="qclass">Q. No.1) This is test question 112
            <img id ="showAns1" class="icons" src="img/expand.png" onclick="javascript:showAns(1)" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns1" class="icons" src="img/collapse.png" onclick="javascript:showAns(1)" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <img alt="image" class="img" src="img/sample.jpg">
        <hr />
        <span id="ans1">This is test answer 112</span>
    </div>
    <br>

    <div class="qbox">
        <span class="qclass">Q. No.2) This is test question 110
            <img id ="showAns2" class="icons" src="img/expand.png" onclick="javascript:showAns(2)" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns2" class="icons" src="img/collapse.png" onclick="javascript:showAns(2)" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <hr />
        <span id="ans2">This is test answer 110</span>
    </div>
    <br>

problem here is that I have to click thrice the image first time then only expand collapse works fine. 这里的问题是我必须第一次单击三次图像,然后才可以展开折叠。 Once I click the image twice after that expand collapse works fine. 一旦我单击图像两次,然后展开折叠效果很好。 I don't know what I am doing wrong, any help will be highly appreciated. 我不知道自己在做什么错,任何帮助将不胜感激。 Thank you. 谢谢。

I would recommend you to use class selector and bind events using jQuery. 我建议您使用类选择器并使用jQuery绑定事件。 Here is an example. 这是一个例子。

HTML 的HTML

<div class="qbox">
    <span class="qclass">QUESTION
        <img class="show icons" src="img/expand.png" alt="show" >
        <img class="hide icons" src="img/collapse.png" alt="hide" >
    </span>
    <hr />
    <span class="ans">Answer</span>
</div>

Script 脚本

$('.qbox .show').click(function(){
    var closestQbox = $(this).closest('.qbox');
    closestQbox.find('.ans').show(500);
    closestQbox.find('.hide').show();
    $(this).hide();
});
$('.qbox .hide').click(function(){
    var closestQbox = $(this).closest('.qbox');
    closestQbox.find('.ans').hide(500);
    closestQbox.find('.show').show();
    $(this).hide();
});

A simplified version of script 脚本的简化版本

$(".ans, .hide").hide();
$('.qbox .icons').click(function () {
    var closestQbox = $(this).closest('.qbox');
    closestQbox.find('.ans, .hide, .show').toggle();
});

DEMO 演示

You can simply do like this 你可以这样简单地做

$("[id^=ans]").hide();
$(".icons").click(function(){
    $(this).closest("span").find("img").toggle();
    $(this).closest(".qbox").find("[id^=ans]").slideToggle();
});

Fiddle 小提琴

Try this code : 试试这个代码:

$(function(){
  $('.icons').click(function(){
        $(this).hide();
        $(this).siblings().show();
        $(this).closest('.qbox').find('[id^=ans]').toggle(500);
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM