恐怕,当我使用代码时:
<div id="sentence_xx"></div>
我总是要打印很多 jquery 代码。
我可以在这个链接中看到代码:http:
//jsfiddle.net/hBRNy/2/
问题是,会有很多 div 语句
<div id="sentence_xx"></div>
每次我想打印它时,我都必须为每个< div>打印整个 jquery 块。
有人知道我怎么解决这个问题吗?因为 jquery 生成的 id 必须是唯一的,我希望这可以更灵活但不知道如何。
亲切的问候
我通过使用 jquery 通配符找到了一个解决方案(你不能用类替换 id,因为 ui.jquery 不再起作用:
<script>
$(document).ready(function() {
// For everey id, started with sentence_ (the sentences which I want)
$('[id^=sentence_]').each(function() {
// strip the id until i have the number only (sentence_ is a static value)
var currentId = $(this).attr('id');
var toRemove = "sentence_";
var id = currentId.replace(toRemove,'');
//replace all the characters by "</li> <li class=\"ui-widget-content_"+id+"\">"
var text = $("#"+currentId).text().trim().split("").join("</li> <li class=\"ui-widget-content_"+id+"\">");
$("#"+currentId).html("<li class=\"ui-widget-content_"+id+"\">" + text + "</li>");
//create a <ol> after the div selectable_xx
$('<ol class="selectable_style" id="selectable_'+id+'"></ol>').insertAfter("#"+currentId);
// remove the value of the div and place them into the <ol>
$('.ui-widget-content_'+id+'').clone().appendTo("#selectable_"+id);
$('#sentence_'+id).remove();
//replace all the " " by non breaking spaces
$('#selectable_'+id+' li').each(function() {
$(this).html($(this).text().replace(' ', ' '));
});
//attache the function for selecting items and put the character place into the next span
$( "#selectable_"+id ).selectable({
stop: function() {
var woord ="" ;
var result = $( "#selectable_"+id ).next('span').empty();
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable_"+ id +" li" ).index( this );
result.append( " #" + ( index + 1 ) );
letter = index + 1;
woord += letter+'';
});
}
});
});
});
</script>
如果你想玩它,我已经更新了代码
http://jsfiddle.net/hBRNy/16/