将href值赋给div

时间:2015-09-21 08:29:05

标签: javascript html twitter-bootstrap

我正在使用Bootstrap手风琴,它有以下代码:

 <div class="panel-heading" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">

我想动态创建手风琴,但我无法在javascript中将.href值设置为div。

var accordionHead = document.createElement("div");
accordionHead.className = "panel-heading";
accordionHead.dataset.parent = "#accordion";
accordionHead.dataset.toggle = "collapse";
accordionHead.href = "#id_"+player.pos;

每个其他选项都会受到重视,而不是.href,因此手风琴不起作用。是否还有其他选项可以将.href值赋予div?

3 个答案:

答案 0 :(得分:1)

但是,您使用的是jQuery,可以使用.attr('href','some value')

来实现

&#13;
&#13;
 $('#accordion_id').attr('href', "#id_player_Position");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div id='accordion_id'>I am accordion, Inspect Me to see the href attribute and value <div/>
&#13;
&#13;
&#13;

注意:href不是div的有效属性。

答案 1 :(得分:1)

var accordionHead = document.createElement("div");
accordionHead.className = "panel-heading";
accordionHead.dataset.parent = "#accordion";
accordionHead.dataset.toggle = "collapse";
accordionHead.setAttribute("href","#id_"+player.pos);`

答案 2 :(得分:0)

无法做到。您应该使用a代码,或使用javascript来操纵location.hash