2013-01-03 53 views
-1

编辑:原来api产生报价。Javascript显示引号中的文本。我如何删除它?

对不起,因为我几乎没有js/jquery的知识,但是我找到了一个我想使用的代码。它使用tumblr的api。 http://www.tumblr.com/docs/en/api/v1

现在,问题是标题[我]显示包裹在引号中的文字。因此,而不是

<div class="relates">This is the titles i. I want this.</div>

它显示

<div class="relates">"This is the titles i. I don't want the quotes!"</div>

在html

。我不希望这样(因为不值得解释的原因)。

有人可以帮我删除引号吗?

以下是完整的代码。

(function() { 
var config = new Object(); 
var titles = []; 
var links = []; 
var images = []; 
var items = []; 
var types = []; 
var $j = jQuery.noConflict() 

var scripts = document.getElementsByTagName('script'); 
var this_script = scripts[scripts.length - 1]; 
var params = this_script.src.replace(/^[^\?]+\??/,'').split('&'); 

var url_base = ((typeof(config.url) == 'undefined') ? ('http://' + document.domain + '/') : ('http://' + config.url + '/')); 

for(var i=0; i<params.length; i++) { 
    var tmp = params[i].split("="); 
    config[tmp[0]] = unescape(tmp[1]); 
} 

if(typeof(config.tags)=='undefined'){ error(0); return; } 
if(typeof(config.num)=='undefined'){ config.num=8; } 
if(typeof(config.len)=='undefined'){ config.len=60; } 
if(typeof(config.title)=='undefined'){ config.title='Related Posts:'; } 
if(typeof(config.type)=='undefined'){ config.type=''; } 

switch(config.css) { 
case ('simple'): 
    document.write('<link rel="stylesheet" type="text/css" ' + 
    'href="http://example.com/resource/simple.css" media="screen" />'); 
break; 
case ('complete'): 
    document.write('<link rel="stylesheet" type="text/css" ' + 
    'href="http://example.com/resource/complete.css" media="screen" />'); 
break; 
case ('light'): 
    document.write('<link rel="stylesheet" type="text/css" ' + 
    'href="http://example.com/resource/light.css" media="screen" />'); 
break; 
case ('dark'): 
    document.write('<link rel="stylesheet" type="text/css" ' + 
    'href="http://example.com/resource/dark.css" media="screen" />'); 
break;} 

    document.write(
    '<div id="tumblrinlink">' + 
     '<div id="inlink-loading"></div>' + 
     '<div id="inlink-title"></div>'+ 
     '<ul id="inlink-list"></ul>' + 
     '' + 
    '</div>' 
); 

var tags = config.tags.slice(0,-1).split(','); 

$j(document).ready(function() { 
    function getRelated() { 
     var req; 
     for(var i=0; i<tags.length; i++){ 
      req=$j.getJSON(url_base+'api/read/json?callback=?&filter=text&num='+config.num+'&start=0&type='+config.type+'&tagged='+escape(tags[i]), function(data) { 
       $j(data.posts).each(function(i, post) { 
        var text=''; 
        if(post.type=='regular') text=post['regular-title']+''+post['regular-body']; 
        else if(post.type=='link') text+=post['link-text']; 
        else if(post.type=='quote') text+=post['quote-text']; 
        else if(post.type=='photo') text+=post['photo-caption']; 
        else if(post.type=='conversation') text+=post['conversation-title']; 
        else if(post.type=='video') text+=post['video-caption']; 
        else if(post.type=='audio') text+=post['audio-caption']; 
        else if(post.type=='answer') text+=post['question']; 
        if(text.length>config.len){ text=text.slice(0,config.len); text+='...';} 
        var image =''; 
        if(post.type=='photo') image+=post['photo-url-100']; 
        else if(post.type=='link') image+=['link-text']; 
        else if(post.type=='quote') image+=['quote-text']; 
        else if(post.type=='photo') image+=['photo-caption']; 
        else if(post.type=='conversation') image+=['conversation-title']; 
        else if(post.type=='video') image+=['video-caption']; 
        else if(post.type=='audio') image+=['audio-caption']; 
        else if(post.type=='answer') image+=['question']; 
        titles.push(text); 
        links.push(post['url-with-slug']); 
        images.push(image); 
        types.push(post['type']) 
       }); 

      }).complete(getList); 
     } 

    } 
    function getList(){ 
     for(var i=0; i<titles.length; i++){ 
      var regex = new RegExp('('+links[i]+')'); 
      var html = $j("#inlink-list").html(); 

      if(links[i]!=document.location&&!html.match(regex)){ 
       if(config.num--<=0) return; 

       var item='<li class="inlink-item" id="'+types[i]+'"><a class="inlink-link" href="'+links[i]+'"><img src="'+images[i]+'" alt="'+titles[i]+'" /><div class="relates">'+titles[i]+'</div></a></li>'; 
       $j("#inlink-list").append(item); 
      } 
     } 
     $j("#inlink-title").html('<h2>'+config.title+'</h2>'); 
     $j("#inlink-loading").html(''); 
    } 
    getRelated(); 

}); 

function getError(e){ 
    var msg="error: "; 
    switch(e){ 
     case 0: msg+='no tags defined'; break; 
     case 1: msg+='tumblr API problem'; break; 
    } 
    $j("#inlink-loading").html(msg); 
}})(); 
+0

是来自Tumblr api的报价?如果是这样,你可以做的不多,但可以剥离它们。 – jbabey

+0

我不知道。顺便说一句,你对大卫是正确的,它只是增加了更多的引号。 我的努力是徒劳吗? – user1411876

+0

显示接收到的数据的样本......没有多大意义API会添加额外的引号 – charlietfl

回答

3

我打算假设报价来自Tumblr API。在这种情况下,你只需要剥去引号:

<div class="relates">'+titles[i].substring(1, titles[i].length - 2)+'</div> 

substring(1, titles[i].length - 2)将删除第一个字符(索引0),并从字符串的最后一个字符(索引length - 1)。

Docs

+0

圣洁的废话!我一直玩弄像猴子一样的JavaScript,认为引号是在javascript中的某处。从来没有想过这件事。 非常感谢,先生。 – user1411876

相关问题