如何使用 jquery 或 javascript 包装动态选择的 id

发布于 2024-12-01 11:03:14 字数 2095 浏览 0 评论 0原文

我正在 Google Blogger 中从事一个项目。首先我想解释一件事。

在博客中,创建的每个帖子都有一个由博客本身分配的唯一 ID。可以使用 Blogger JSON 检索此 ID。所以我使用 JSON 检索了最近四篇帖子的 ID。

我想使用 JQuery 或 Javascript 将前四个 id 容器包装在 DIV 容器周围。

问题是,当我在选择器 $ 中绝对使用这些 id 并使用 wrapAll() 函数时,id 容器会被包裹起来。

但正如我所说,我使用 JSON 来获取容器 id,因此值ID 存储在变量中,当我使用这些变量作为 wrapAll() 函数的选择时,它不起作用。

我有这两种情况的演示,可以通过访问此博客 http://youblog-demo.blogspot 来查看。 com/ 并使用 firebug 控制台运行这些代码。

当我使用绝对容器 ID 时的情况 1

var script = document.createElement("script");
script.src = "http://youblog-demo.blogspot.com/feeds/posts/default?alt=json&callback=hello";
document.body.appendChild(script);
function hello(json){
if(json.feed.entry.length>4){
var post_num=4;
var id_coll = new Array();
for(i=0; i<post_num; i++){
    var ids = json.feed.entry[i].id.$t;
    var post_id = ids.substring(ids.indexOf("post-"));
    var only_id = post_id.substring(5);
    id_coll[i] = only_id;
}
$("#3337831342896423186,#123892177945256656,#9095347670334802803,#2525451832509945787").wrapAll('<div>');
}
};


当我使用变量来选择容器时的情况 2

var script = document.createElement("script");
script.src = "http://youblog-demo.blogspot.com/feeds/posts/default?alt=json&callback=hello";
document.body.appendChild(script);
function hello(json){
if(json.feed.entry.length>4){
    var post_num=4;
    var id_coll = new Array();
    var front_name = "#";
    for(i=0; i<post_num; i++){
        var ids = json.feed.entry[i].id.$t;
        var post_id = ids.substring(ids.indexOf("post-"));
        var only_id = post_id.substring(5);
        id_coll[i] = only_id;
    }
var joined_id_0 = String.concat(front_name,id_coll[0]);
var joined_id_1 = String.concat(front_name,id_coll[1]);
var joined_id_2 = String.concat(front_name,id_coll[2]);
var joined_id_3 = String.concat(front_name,id_coll[3]);
$(joined_id_0,joined_id_1,joined_id_2,joined_id_3).wrapAll('<div>');
}
};


因此,当我使用情况 2 代码时,它不起作用,但情况 1 代码工作正常。谁能帮我解决这个问题

I am working on a project in Google Blogger. First i want to explain a thing.

In blogger every post that is created has a unique id assigned to it by blogger itself. This id can be retrieved using Blogger JSON. So i have retrieved the ids of four recent posts using JSON.

I want to wrap these first four id containers around a DIV container using JQuery or Javascript.

The problem is when i use these ids absolutely in the selector $ and use the wrapAll() function the id container's gets wrapped up.

But as i said i'm using JSON to get the container id's so the values of ID's are stored in variable's and when i use those variable as selection for wrapAll() function it doesn't work.

I have demos of both those situation's which can be seen by going to this blog http://youblog-demo.blogspot.com/ and using the firebug console to run these code.

Situation 1 when i use absolute container ids

var script = document.createElement("script");
script.src = "http://youblog-demo.blogspot.com/feeds/posts/default?alt=json&callback=hello";
document.body.appendChild(script);
function hello(json){
if(json.feed.entry.length>4){
var post_num=4;
var id_coll = new Array();
for(i=0; i<post_num; i++){
    var ids = json.feed.entry[i].id.$t;
    var post_id = ids.substring(ids.indexOf("post-"));
    var only_id = post_id.substring(5);
    id_coll[i] = only_id;
}
$("#3337831342896423186,#123892177945256656,#9095347670334802803,#2525451832509945787").wrapAll('<div>');
}
};

Situation 2 when i use variable's to select the containers

var script = document.createElement("script");
script.src = "http://youblog-demo.blogspot.com/feeds/posts/default?alt=json&callback=hello";
document.body.appendChild(script);
function hello(json){
if(json.feed.entry.length>4){
    var post_num=4;
    var id_coll = new Array();
    var front_name = "#";
    for(i=0; i<post_num; i++){
        var ids = json.feed.entry[i].id.$t;
        var post_id = ids.substring(ids.indexOf("post-"));
        var only_id = post_id.substring(5);
        id_coll[i] = only_id;
    }
var joined_id_0 = String.concat(front_name,id_coll[0]);
var joined_id_1 = String.concat(front_name,id_coll[1]);
var joined_id_2 = String.concat(front_name,id_coll[2]);
var joined_id_3 = String.concat(front_name,id_coll[3]);
$(joined_id_0,joined_id_1,joined_id_2,joined_id_3).wrapAll('<div>');
}
};

So when i use the situation 2 code then it doesn't work but the situation1 code works fine. Can anybody help me with this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2024-12-08 11:03:14

您需要将选择器作为字符串而不是参数列表传递;

$(joined_id_0+', '+joined_id_1+', '+joined_id_2+', '+joined_id_3).wrapAll('<div>');

或者更好的是,将所有: 替换

var joined_id_0 = String.concat(front_name,id_coll[0]);
var joined_id_1 = String.concat(front_name,id_coll[1]);
var joined_id_2 = String.concat(front_name,id_coll[2]);
var joined_id_3 = String.concat(front_name,id_coll[3]);
$(joined_id_0,joined_id_1,joined_id_2,joined_id_3).wrapAll('<div>');

为:

$('#'+id_coll.join(', #')).wrapAll('<div>');

并删除行: var front_name = '#';

You need to pass in the selector as a string, not a list of arguments;

$(joined_id_0+', '+joined_id_1+', '+joined_id_2+', '+joined_id_3).wrapAll('<div>');

Or even better, replace all of:

var joined_id_0 = String.concat(front_name,id_coll[0]);
var joined_id_1 = String.concat(front_name,id_coll[1]);
var joined_id_2 = String.concat(front_name,id_coll[2]);
var joined_id_3 = String.concat(front_name,id_coll[3]);
$(joined_id_0,joined_id_1,joined_id_2,joined_id_3).wrapAll('<div>');

With:

$('#'+id_coll.join(', #')).wrapAll('<div>');

And remove the line: var front_name = '#';

〃安静 2024-12-08 11:03:14

您必须连接 ids,并用逗号分隔,如 #id1, #id2, ... 所示。

您可以这样做:

[joined_id_0,joined_id_1,joined_id_2,joined_id_3].join(',')

整行:

$([joined_id_0,joined_id_1,joined_id_2,joined_id_3].join(',')).wrapAll('<div>');

如果不起作用,请检查 [joined_id_0,joined_id_1,joined_id_2,joined_id_3].join(',') 返回的内容 (alert( )它,或使用console.log)。

You have to concatenat the ids, separated by a comma, as in #id1, #id2, ....

You can do that this way:

[joined_id_0,joined_id_1,joined_id_2,joined_id_3].join(',')

The whole line:

$([joined_id_0,joined_id_1,joined_id_2,joined_id_3].join(',')).wrapAll('<div>');

If it doesn't works, check the what is returned by [joined_id_0,joined_id_1,joined_id_2,joined_id_3].join(',') (alert() it, or use console.log).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文