javascript 属性值可以通过手动 url 参数确定吗?

发布于 2024-10-14 09:15:08 字数 590 浏览 2 评论 0原文

我正在尝试显示来自外部 .jsp 文件的数据,该文件的设置如下:

<tag>
  <innertag1 id="1">
  <innertag1 id="2">
</tag>
<tag>
  <innertag2 id="3">
  <innertag2 id="4">
</tag>

要仅显示来自一个特定“innertag”标签的信息,我当前正在使用:

NodeList labs = XMLInfo.getElementsByTagName("innertag1");

我希望能够隔离任何轻松地使用特定标签。理论上,我可以创建许多单独的页面,然后简单地将值更改为“innertag2”、“innertag3”等,但这显然有点不切实际。

有没有办法通过 URL 参数确定值?例如,如果我只想显示“innertag2”中的数据,有没有办法让 url http://www.server.com/data.jsp?id=innertag2 调整标记名适当地?

谢谢,任何帮助将不胜感激。

I am trying to display data from an external .jsp file, which is set up something like this:

<tag>
  <innertag1 id="1">
  <innertag1 id="2">
</tag>
<tag>
  <innertag2 id="3">
  <innertag2 id="4">
</tag>

To display only information from only one particular "innertag" tag, I'm currently using:

NodeList labs = XMLInfo.getElementsByTagName("innertag1");

I'd like to be able to isolate any particular tag with ease. Theoretically, I could create many individual pages and simply change the values to "innertag2," "innertag3," etc., but this is obviously a bit impractical.

Is there a way to determine the value via a URL parameter? For instance, if I wanted to only display data from "innertag2," is there a way that the url http://www.server.com/data.jsp?id=innertag2 would adjust the tagname properly?

Thank you, any help would be much appreciated.

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

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

发布评论

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

评论(2

千柳 2024-10-21 09:15:08

您可以解析 document.location.href 并从中提取参数。这是来自我使用此技术的旧 HTML 文件(但是不确定它是否在所有浏览器上兼容)。

var args = {};

function parseArgs()
{
    var aa = document.location.href;
    if (aa.indexOf("?") != -1)
    {
        aa = aa.split("?")[1].split("&");
        for (var i=0; i<aa.length; i++)
        {
            var s = aa[i];
            var j = s.indexOf("=");
            if (j != -1)
            {
                var name = s.substr(0, j);
                var value = s.substr(j + 1);
                args[name] = value;
            }
        }
    }
}

You can parse document.location.href and extract parameters from there. This is from an old HTML file where I used this technique (not sure if it's compatible on all browsers, however).

var args = {};

function parseArgs()
{
    var aa = document.location.href;
    if (aa.indexOf("?") != -1)
    {
        aa = aa.split("?")[1].split("&");
        for (var i=0; i<aa.length; i++)
        {
            var s = aa[i];
            var j = s.indexOf("=");
            if (j != -1)
            {
                var name = s.substr(0, j);
                var value = s.substr(j + 1);
                args[name] = value;
            }
        }
    }
}
半世晨晓 2024-10-21 09:15:08

不确定这是否是您要查找的内容,但您可以使用 location.search 从网址访问参数。

6502的答案几乎足够好了,它不是url解码参数。下面的函数更加精致(描述性变量名称,没有全局变量)

function getUrlParams() {

  var paramMap = {};
  if (location.search.length == 0) {
    return paramMap;
  }
  var parts = location.search.substring(1).split("&");

  for (var i = 0; i < parts.length; i ++) {
    var component = parts[i].split("=");
    paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);
  }
  return paramMap;
}

然后你可以这样做

var params = getUrlParams();
XMLInfo.getElementsByTagName(params['id']); // or params.id

Not sure if this is what you're looking for, but you can access parameters from the url using location.search.

6502's answer is almost good enough, it's not url decoding parameters. The function below is a bit more polished (descriptive variable names, no global variables)

function getUrlParams() {

  var paramMap = {};
  if (location.search.length == 0) {
    return paramMap;
  }
  var parts = location.search.substring(1).split("&");

  for (var i = 0; i < parts.length; i ++) {
    var component = parts[i].split("=");
    paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);
  }
  return paramMap;
}

Then you could do

var params = getUrlParams();
XMLInfo.getElementsByTagName(params['id']); // or params.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文