jQuery缩短代码

时间:2013-03-19 15:55:34

标签: jquery

我想缩短这段代码,所以我想制作3行或4行,

但是如果我尝试制作3行,那么它不会起作用。

$('#uebersicht').append('<ul data-role="listview" data-split-icon="gear" data-split-theme="d" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow"><li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="false" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li ui-li-has-alt ui-li-has-thumb ui-first-child ui-last-child ui-btn-up-c"><div class="ui-btn-inner ui-li ui-li-has-alt"><div class="ui-btn-text"><a href="#" class="ui-link-inherit"><img class="ui-li-thumb" src='+icerik.Resim+'><h2 class="ui-li-heading">'+moschee+'</h2><p class="ui-li-desc">'+results[0].formatted_address+'</p><p class="ui-li-desc">'+hesapla(meineLongitude,meineLatitude,icerik.Position.Longitude,icerik.Position.Latitude)+'</p></a></div></div><a href="#purchase" data-rel="popup" data-position-to="window" data-transition="pop" title="Purchase album" class="ui-li-link-alt ui-btn ui-btn-up-c ui-btn-icon-notext" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="false" data-iconpos="notext" data-theme="c" aria-haspopup="true" aria-owns="#purchase"><span class="ui-btn-inner"><span class="ui-btn-text"></span><span data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="gear" data-iconpos="notext" data-theme="d" title="" class="ui-btn ui-btn-up-d ui-shadow ui-btn-corner-all ui-btn-icon-notext"><span class="ui-btn-inner"><span class="ui-btn-text"></span><span class="ui-icon ui-icon-gear ui-icon-shadow">&nbsp;</span></span></span></span></a></li></ul>');

4 个答案:

答案 0 :(得分:1)

使用\字符转义您的javascript换行符,如下所示:

$('#uebersicht').append('<ul data-role="lisview" data-split-icon="gear"\
 data-split-theme="d" data-inset="true" class="ui-listview \
ui-listview-inset ui-corner-all ui-shadow">');

上面是你的长字符串的缩短版本,但你明白了,使用\打破javascript新行。

答案 1 :(得分:0)

当您需要附加多个HTML元素时,最好的方法是将其排序并清理为以这种方式拆分.append()

$('#myID').append('<p>lorem ipsum</p>')
          .append('<p>dolor sit amet...</p>')
          .append('etc...');

但不是很好,附加太多元素......

答案 2 :(得分:0)

将它拆分到任何你想要的地方。只需在任何地方结束字符串,添加加号,换行符,然后再次启动字符串。 e.g。

$('#uebersicht').append(
    '<ul data-role="listview" data-split-icon="gear" ' +
        'data-split-theme="d" data-inset="true" ' +
        'class="ui-listview ui-listview-inset ui-corner-all ui-shadow">' +
        '<li data-corners="false" data-shadow="false" data-iconshadow="true"' +
             'data-wrapperels="div" data-icon="false" data-iconpos="right"' +
             'data-theme="c" class="ui-btn ui-btn-icon-right ui-li ' +
             'ui-li-has-alt ui-li-has-thumb ui-first-child ui-last-child ' +
             'ui-btn-up-c">' +
          '<div class="ui-btn-inner ui-li ui-li-has-alt">' +
          '<div class="ui-btn-text">' +
          '<a href="#" class="ui-link-inherit">' +
          '<img class="ui-li-thumb" src='+icerik.Resim+'>' +
          '<h2 class="ui-li-heading">'+moschee+'</h2>' +
          '<p class="ui-li-desc">' +
              results[0].formatted_address+ 
          '</p> ' +
          '<p class="uii-desc">' +   
            hesapla(meineLongitude, eineLatitude, icerik.Position.Longitude,       
               icerik.Position.Latitude) +
          '</p></a></div></div>' +
          '<a href="#purchase" data-rel="popup" data-position-to="window" ' +
            'data-transition="pop" title="Purchase album" ' +
            'class="ui-li-link-alt ui-btn ui-btn-up-c ui-btn-icon-notext"' +
            'data-corners="false" data-shadow="false" data-iconshadow="true" ' +
            'data-wrapperels="span" data-icon="false" data-iconpos="notext" ' +
            'data-theme="c" aria-haspopup="true" aria-owns="#purchase"> ' +
          '<span class="ui-btn-inner">' +
          '<span class="ui-btn-text"></span>' +
          '<span data-corners="true" data-shadow="true" data-iconshadow="true" ' +
             'data-wrapperels="span" data-icon="gear" data-iconpos="notext" ' +
             'data-theme="d" title="" class="ui-btn ui-btn-up-d ui-shadow ' +
             'ui-btn-corner-all ui-btn-icon-notext">' +
          '<span class="ui-btn-inner">' +
          '<span class="ui-btn-text"></span>' +
          '<span class="ui-icon ui-icon-gear ui-icon-shadow">&nbsp;' +
          '</span></span></span></span></a></li></ul>'
);

我可以做得更干净,有更好的缩进,但你应该明白这个想法。根据我的理解,这比在每一行调用append更好 - 每个追加都需要相对较长的时间来完成。

答案 3 :(得分:0)

最干净的解决方案可能是创建一个jquery容器,通过append / appendTo添加所有元素,并将整个对象插入到dom中,以避免过多的dom操作/重绘。

但是使用你的html字符串,这可能会产生相当长的脚本......

var $list = $('<ul>');

$list.data({
    'role': 'listview',
    'split-icon': 'gear',
    // etc...
}).addClass('ui-listview ui-listview-inset ui-corner-all ui-shadow');

$li = $('<li>');
// take the base object and clone it if used multiple times
$li.clone().data({
    // fill up data, add classes etc.
}).addClass('classes').appendTo($list);

// and so on with each element, use all the beautiful jquery functions available
// there's no dom manipulation to this point, performance still fine
// now in the end, insert the object into the dom:
$list.appendTo('#uebersicht');

也许更好的方式:

var html = '';

html += '<ul data-role="listview" data-split-icon="gear" data-split-theme="d" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">';
html += '<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="false" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li ui-li-has-alt ui-li-has-thumb ui-first-child ui-last-child ui-btn-up-c">';
html += '<div class="ui-btn-inner ui-li ui-li-has-alt">';

// etc. just make a line for each tag or split it if too long

$('#uebersicht').append(html);