1. 程式人生 > >記錄unterminated string literal報錯解決

記錄unterminated string literal報錯解決

//擷取字串 str字串 len擷取的長度
function cut_str(id, len){
    var str=$("#"+id+" a:eq(0)").attr("title");
    var char_length = 0;
    if(str.length<=len)
     return str;
    for (var i = 0; i < str.length; i++){
        var son_str = str.charAt(i);
        encodeURI(son_str).length > 2 ? char_length += 1 : char_length += 0.5;
        if (char_length >= len){
            var sub_len = char_length == len ? i+1 : i;
            return str.substr(0, sub_len);
        }
    }
    return str;
}

function cut_str2(str, len){
    var char_length = 0;
    if(str.length<=len)
     return str;
    for (var i = 0; i < str.length; i++){
        var son_str = str.charAt(i);
        encodeURI(son_str).length > 2 ? char_length += 1 : char_length += 0.5;
        if (char_length >= len){
            var sub_len = char_length == len ? i+1 : i;
            return str.substr(0, sub_len);
        }
    }
    return str;
}

<#list listme as jsxx>                             

          <li class="jsli" id="js${jsxx.id}">
                              <a style="text-decoration:none;font-size: 11px;" href="#" title="${jsxx.content}">
                                    <script type="text/javascript">

                                        document.write(cut_str("js${jsxx.id}",15));

                                       /*document.write(cut_str2("${jsxx.content}",15));
                                        這種寫法如果字串中有回車就會報錯,如下
                                        document.write(cut_str("aaa
bbbb
cccc",15));
                                       */

                                     //document.write(cut_str2($("#${jsxx.content} a:eq(0)").attr("title"),15));這種寫法有回車不報錯

                                    </script>

                                  <!-- <#if jsxx.content?length gt 14>${jsxx.content?substring(0,14)}<#else>${jsxx.content}</#if>

                                         使用這種方法顯示內容英文比中文顯示的長度要短-->

                              </a>
                              <span class="pull-right">
                              <#if date_3?date lt jsxx.mainTime?date>                              
                              <span style="font-size: 12px;color: red;">new</span>
                              </#if>
                              ${jsxx.mainTime?string('MM/dd')}</span>
            </li>
 </#list>