-1

编辑:原来api会产生引号。

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

现在,问题是titles[i] 显示用引号括起来的文本。所以而不是

<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);
}})();
4

1 回答 1

3

我将假设引号来自 Tumblr API。在这种情况下,您只需要去掉引号:

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

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

文档

于 2013-01-03T02:07:35.390 回答