简体   繁体   English

如何动态更改jquery ui手风琴的内容选项卡

[英]How can i change dynamically the content tab of jquery ui accordion

I have the next code: 我有下一个代码:

<div id="accordion">
    <h3>Row 1</h3>
    <div id="1"></div>
    <h3>Row 2</h3>
    <div id="2"></div>
    <h3>Row 3</h3>
    <div id="3"></div>
</div>

$("#accordion").accordion({
    autoHeight: false,
    clearStyle: true,
    active: 0,
    change: function(event, ui) {
    $("div#2").append('<p>Test</p>');
    }
});

When the accordion is clicked in another row, i want to fill the <div id="2"> with data. 当在另一行中单击手风琴时,我想用数据填充<div id="2"> On change event i am trying this: 在改变事件我正在尝试这个:

$("div #2").append('<p>Test</p>');

But the text "Test" is printed inside the h3 instead the div... how can i achieve to print the text on the div??? 但文本“测试”打印在h3内而不是div ...我怎样才能实现在div上打印文本???

I am using jquery 1.9.0 我正在使用jquery 1.9.0

It seems like a bug http://bugs.jqueryui.com/ticket/4469#comment:4 isnt it? 这似乎是一个bug http://bugs.jqueryui.com/ticket/4469#comment:4不是吗?

You'll have to redo the values you set for the id attributes. 您必须重做为id属性设置的值。 Try: 尝试:

<div id="accordion">
    <h3 id="header1">Row 1</h3>
    <div id="content1"></div>
    <h3 id="header2">Row 2</h3>
    <div id="content2"></div>
    <h3 id="header3">Row 3</h3>
    <div id="content3"></div>
</div>

That way, you can easily target which: 这样,您可以轻松地定位哪个:

$("#content2").append('<p>Test</p>');

(or actually use .append() ) (或实际使用.append()

This is because the id attribute must be unique in order to target the element you actually want. 这是因为id属性必须是唯一的,以便定位您真正想要的元素。 jQuery will use the native document.getElementById method that will return the first found element with that id . jQuery将使用本机document.getElementById方法,该方法将返回具有该id第一个找到的元素。

UPDATE: 更新:

Per your new edit, you need to use the selector without a space: 根据您的新编辑,您需要使用没有空格的选择器:

$("div#2").append('<p>Test</p>');

Although you do not need to include the div part of the selector. 虽然您不需要包含选择器的div部分。 Just use: 只需使用:

$("#2").append('<p>Test</p>');

While HTML5 does allow the id to start with a number (and be 1 character long), I would suggest using a more descriptive id , like before I said: "content1", etc. 虽然HTML5确实允许id以数字开头(并且长度为1个字符),但我建议使用更具描述性的id ,就像之前我说的那样:“content1”等。

UPDATE 2: 更新2:

Looking it over again, the $("div #2") should actually work as well, since the element with the id "2" is in a <div> . 再次看它结束了, $("div #2")应实际工作为好,因为与元素id “2” <div> So technically that selector should work. 从技术上讲,选择器应该工作。 Nonetheless, I'd still suggest a selector that isn't a number and is more descriptive. 尽管如此,我仍然建议选择器不是数字,而是更具描述性。

With your code, I got it working with a more descriptive selector anyways: http://jsfiddle.net/BrRPz/2/ 使用您的代码,无论如何我使用了更具描述性的选择器: http//jsfiddle.net/BrRPz/2/

In css, id selector is unique element. 在css中,id选择器是唯一的元素。 so don't repeat a same id in the page 所以不要在页面中重复相同的ID

<div id="accordion">
    <h3 id="1">Row 1</h3>
    <div class="1"></div>
    <h3 id="2">Row 2</h3>
    <div class="2"></div>
    <h3 id="3">Row 3</h3>
    <div class="3"></div>
</div>

use script as 使用脚本作为

$("div.2").append('<p>Test</p>');

Note: id selector is unique element. 注意: id选择器是唯一元素。

Try $("div#2").append('<p>Test</p>'); 尝试$("div#2").append('<p>Test</p>');

$("div #2") will target an element with id='2' inside of a <div> element, whereas $("div#2") targets a <div> with id='2' . $("div #2")将在<div>元素内定位id='2'的元素,而$("div#2")定位id='2'<div>

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

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