我有这段代码:
<div class="time-widget">
<select id="p_h_0_startHour_hour" name="" class="form-control select-time form-control">
<option value="0">
00
</option>
........
</select>
</div
从java脚本我想在div
class='time-widget'
之前追加html和html:
$('<div>hello</div>').appendTo('#p_h_'+i+'_startHour_hour').parent();
但是这个html正在添加到time-widget
div前面的选择框中。有什么建议吗?Thx提前。
答案 0 :(得分:0)
将$('<div>hello</div>').appendTo('#p_h_'+i+'_startHour_hour').parent();
更改为$('<div>hello</div>').insertBefore($('#p_h_'+i+'_startHour_hour').parent());
您必须将其附加到元素。
<强>演示强>
$('<div>hello</div>').insertBefore($('#p_h_0_startHour_hour').parent());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="time-widget">
<select id="p_h_0_startHour_hour" name="" class="form-control select-time form-control">
<option value="0">
00
</option>
</select>
</div>
&#13;
演示2
$('.test').remove();
$('<div class="test">hello</div>').insertBefore($('#p_h_0_startHour_hour').parent());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="time-widget">
<select id="p_h_0_startHour_hour" name="" class="form-control select-time form-control">
<option value="0">
00
</option>
</select>
</div>
&#13;
答案 1 :(得分:0)
你想要的是在$('#p_h_'+i+'_startHour_hour')
之前插入,因为'#p_h_'+i+'_startHour_hour'
只是一个文本,你忘了放$
并说你想要它之前(使用{{1} }})
insertBefore()
&#13;
var i = 0;
$('<div>hello</div>').insertBefore(($('#p_h_'+i+'_startHour_hour').parent()));
&#13;
答案 2 :(得分:0)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="time-widget">
<select id="p_h_0_startHour_hour" name="" class="form-control select-time form-control">
<option value="0">
00
</option>
........
</select>
</div>
&#13;
$('<div>hello</div>').insertBefore('.time-widget');
&#13;