传递变量以使用JavaScript或jQuery </div> </h3>创建<h3>和<div>标记

时间:2010-10-19 17:55:52

标签: javascript jquery-ui-accordion

不确定如何使用javascript或jquery使我的div填充手风琴样式标题和内容。

<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.2.custom.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    $("#accordion").accordion(
        autoheight: false);
    });

/**       Variable

   *       variableID:String - unique string identifier for the variable
   *       name:String - plain text name
   *       description:String - description of the variable and its meaning
   *       value:Integer - current number-based value of the variable
   *       locked:Boolean - whether the variable is locked against further modification
*/       
    function loadVariables(variables) {
        if (typeof variables != "object") { alert(variables); return;}
    for (var i = 0; i < variables.length - 1; i++) {
/*  make name= <h3><a href="#">name</a></h3>
    make variableID, description, value=
    <div>
    <p>"Variable ID : " + variableID</br>
    "Description : " +description</br>
    "Initial Value : " +value</br>
    </p>
    <p>"History"</br>
    A container for updateVariable function.
    </p>*/
    }
    };

    function updateVariable(variable) {
    // make update Variable in the History box as variable values change
    // change background color and change locked icon status
    };
    </script>


</head>
<body>
<div id="accordion">
<!--- Divs Created Dynamically here ---->
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

// Inside the for loop, do this:
// This is the <h3> jQuery object you'll be inserting. I'm chaining the creation.
var h3 = $('<h3>').append($('<a>').attr('href', '#').text('name'))
$('#accordion').append(h3)
// This is the <div> object to be inserted into the accordion.
var div = $('<div>').append($('p').html('html inside first <p>'));
div = div.append($('p').html('html for history'))
$('#accordion').append(div)

这里的关键是你可以使用$(parent).append($('<tag-name>')). $('<tag-name>')向DOM添加内容,这将创建一个刚浮动的标记的新jQuery对象。 .append(obj)方法会将obj附加到调用它的父级的末尾。因此$('#accordion').append($('<h3>'))会在#accordion末尾添加一个打开和关闭的h3标记。