getElementById('pcode') 许多元素都已编号

发布于 2024-10-09 17:44:05 字数 395 浏览 3 评论 0原文

我有一个文档,当单击链接时,该表的每一行中都会显示一个结果表,其中有一个带有 SPAN 标记的 TD 元素,其中有一个 id,从 2 开始,最多可以递增到 400(即id=“pcode2”)。

我需要获取其内部 HTML 值并将它们写入 Javascript 函数?、数组?细绳?后跟一个逗号,用于地图的另一个 Javascript 函数。

数据经过预先排序,通过单击链接调用地图来获取路线。 span id 永远不会超过 400。

最终目的地需要如下所示。

var via = ("pcode2," "pcode3,", "pcode4");

其中 pcode 等于 SPAN 的内部 HTML 值(邮政编码)。

谢谢

贾斯汀

I have a a document that when an link is clicked a table of results is displayed within each row of this table there is a TD element with a SPAN tag, inside of this is an id which starts at 2 and can increment up to 400 (i.e. id="pcode2").

I have a need to get the inner HTML values of this and write them into a Javascript function?, array? string? followed by a comma to use on another Javascript function for a map.

The data is pre sorted and the map is called by clicking a link to get the route. the span id's will never reach more than 400.

the end destination needs to look like this..

var via = ("pcode2," "pcode3,", "pcode4");

Where pcode is equal to the innerHTML value of the SPAN which is a postcode.

Thanks

Justin

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

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

发布评论

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

评论(1

探春 2024-10-16 17:44:05

你的意思是这样的:

var values = [];

for(var i = 2; i <= 400; i++) {
    var element = document.getElementById('pcode' + i);
    if(element === null) {
        break;
    }
    values.push(element.innerHTML); // or element.innerHTML + ','
}

如果没有,你应该澄清我们的问题。

但不确定为什么需要逗号。如果您想稍后连接这些值,只需使用 join()

var str = values.join(',');

示例:

var values = ['a','b','c'];
var str = values.join(',');
// gives "a,b,c"

更新: 来自 文档,我认为你必须这样做(假设 map 全局可用):

function GetRoute() { 
    var from = document.getElementById('inpAddr').value; 
    var locations = [from]; 
    for(var i = 2; i <= 400; i++) { 
        var element = document.getElementById('pcode' + i); 
        if(element === null) { break; } 
        locations.push(element.innerHTML); 
    } 
    var options = new VERouteOptions(); 
    options.DrawRoute = true; 
    map.GetDirections(locations,options); 
}

请注意 locations 只是一个数组。在原始代码中,您创建了一个嵌套数组。而且您似乎不需要尾随逗号,只需要一系列位置。

You mean something like:

var values = [];

for(var i = 2; i <= 400; i++) {
    var element = document.getElementById('pcode' + i);
    if(element === null) {
        break;
    }
    values.push(element.innerHTML); // or element.innerHTML + ','
}

?

If not, you should clarify our question.

Not sure why you need the comma though. If you want to concatenate the values later, you can just use join():

var str = values.join(',');

Example:

var values = ['a','b','c'];
var str = values.join(',');
// gives "a,b,c"

Update: From the documentation, I think you have to do this (assuming map is globally available):

function GetRoute() { 
    var from = document.getElementById('inpAddr').value; 
    var locations = [from]; 
    for(var i = 2; i <= 400; i++) { 
        var element = document.getElementById('pcode' + i); 
        if(element === null) { break; } 
        locations.push(element.innerHTML); 
    } 
    var options = new VERouteOptions(); 
    options.DrawRoute = true; 
    map.GetDirections(locations,options); 
}

Note that locations is just a single array. In your original code, you create a nested array. And you don't seem to need a trailing comma, just an array of locations.

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