Jquery动态手风琴:仅使用新添加的面板进行面板标题颜色?

时间:2016-01-11 12:29:08

标签: jquery html styles selector jquery-ui-accordion

我有一个动态的jquery手风琴。我在顶部有2个字段。在填写两个字段并且用户按下"添加问题按钮"之后,在顶部添加一个带有蓝色标题的新面板。随着我不断添加问题,越来越多的面板被添加到前面,它们的标题也是蓝色的。我在我的jquery部分添加了蓝色标题的代码,我将添加我的面板。

我还有一个提交按钮,刷新页面并将页面恢复为默认值(因此所有以前的面板都会丢失)。

以下是输出:https://jsfiddle.net/5gLb0cqx/

我的问题是这样的:我正在努力使新添加的面板有一个蓝色标题。

我的代码如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Accordion - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script>
  var counter = 2;

  $(function() {
    $("#accordion").accordion({
        collapsible: true,
        active: false,
        });

    function edit(){
    var text = $(this).siblings("input[type=text]").val();
    var sql = $(this).siblings('textarea').val();
    $("#input").val(text);
    $("#sql_code").val(sql);
    }
    $(":button").click(edit);

    $("#addAccordion").click( function() {
        var inputVal = $("#input").val();
        var sqlVal = $("textarea#sql_code").val();
        if (!sqlVal || !inputVal){
            return;
        }
        var newDiv = '<div><h3 style = "background:lightblue;">Question '+ counter +'</h3></div>';
        var content = '<div class = "new_panel"><label>'+inputVal+'</label><br><br><label for="in" name="question">Edit Question:</label> <input type="text" name = "question" value ="'+inputVal+'" /><br><br>'
        + ' <label name="SQL">Edit SQL code here:</label><textarea name = "code">'+sqlVal+'</textarea>' +
        '<br><br> <input type = "button" value = "Edit" ></input></div>';
        $("#accordion").prepend(newDiv +content) ;
        $("#accordion").accordion("refresh");
        counter++;
        $(":button").click(edit);
    });
  });
  </script>
</head>
<body>

<center>
<form id="myform">
    <label>Enter Question:</label>
    <input id="input" type="text" name = "questions"/>
    <br><br>
    <label>Enter SQL code here:</label>
    <textarea id="sql_code" name = "SQL_code"></textarea>
    <br><br>
    <input id = "submitbutton" type="submit" value="Submit"/>
    <input type = "button" id ="addAccordion" value = "Add Question" ></input>
</form>
</center>

<div id = "accordion">
    <h3> Question 1 </h3>
    <div>
        <form>
            Have you ever been tested for an STI?
            <br><br>
            <label for="in" name="question1">Edit Question:</label>
            <input type="text"/>
            <br><br>
            <label for="sql" name="SQL">Edit SQL code here:</label>
            <textarea></textarea>
            <br><br>
            <input type = "button" value = "Edit" ></input>
            </form>
    </div>

</div>
</body>
</html>

我不知道如何使用我当前的代码执行此操作。

我想在某种程度上循环使用手风琴面板并找到最新的面板,虽然我不知道如何做到这一点。

任何人都可以指出我正确的方向或对如何解决这个问题有任何建议吗?

2 个答案:

答案 0 :(得分:1)

demo

如果你只在每个面板的标题中有h3,只需在jQuery中添加一行

$("#accordion").find("h3").not(":first").css({"background":"none"})

在预先安排新的小组之后,或者

$("#accordion").find("h3").css({"background":"none"})

在前期之前。

答案 1 :(得分:0)

更改一些jquery

var newDiv = '<div style="background:lightblue;"><h3 style="margin:2px 0 0;">Question '+ counter +'</h3></div>';

https://jsfiddle.net/5gLb0cqx/2/

相关问题