简体   繁体   English

如何使用 onClick 在 function 中编写或替换 url?

[英]How do you write or replace a url in a function using onClick?

I'm new to javascript and I'm having a particularly difficult time figuring out how write new urls into an existing function using onClick.我是 javascript 的新手,我很难弄清楚如何使用 onClick 将新的 url 写入现有的 function。

Here is some more info:这是更多信息:

I want to have something like this我想要这样的东西

   <script type="text/javascript">

    function addurl(){
    "server": [
                 {
                    "position": "center",
                    "url": "New_Url"
                 }
              ]
                     }
    </script>

    <div id="urlbutton"></div>
    <input type="button" onclick="????" value="url123">

Basically I want to replace the "url" when clicking different buttons/links.基本上我想在单击不同的按钮/链接时替换“url”。 I've spent hours and hours today trying to figure this out and now I'm basically all turned around not knowing what to do.我今天花了好几个小时试图弄清楚这一点,现在我基本上都转过身来不知道该怎么做。

Any type of help will be greatly appreciated!任何类型的帮助将不胜感激!

The contents of your function should be a series of statements;你的function的内容应该是一系列语句; your sample is more like (almost) an object literal.您的样本更像(几乎)一个 object 文字。 So that won't run.所以这不会运行。

Intially I'll ignore your almost object literal because I don't understand what you're trying to do.最初,我会忽略您几乎 object 的文字,因为我不明白您要做什么。 Instead, here are two ways that a button can call a function to set the url of the page, ie, cause the browser to navigate away from the current page to that URL.相反,这里有两种方法可以让按钮调用 function 来设置页面的 url,即导致浏览器从当前页面导航到该 ZE6B391A8D2C4D455902A23A8B65D。 The first you pass the URL to the function.首先,您将 URL 传递给 function。 The second you pass a reference to the button to the function and then from that reference access the button's 'value' attribute.第二次将按钮的引用传递给 function,然后从该引用访问按钮的“值”属性。 Plus I've given a way to set the URL without a function.另外,我已经给出了一种在没有 function 的情况下设置 URL 的方法。

Note: I'm not sure that any of these is necessarily a sensible thing to do on a webpage.注意:我不确定这些是否一定是在网页上做的明智之举。 If you want to navigate to another URL use an <a> tag so that it will work for users with no JavaScript.如果您想导航到另一个 URL,请使用<a>标签,以便它适用于没有 JavaScript 的用户。 But anyway...但不管怎么说...

function addUrl(newUrl) {
   window.location.href = newUrl;
}
function addUrl2(btn) {
   window.location.href = btn.value;
}

<input type="button" value="My URL" onclick="addUrl('someURL');">
<input type="button" value="www.google.com" onclick="addUrl2(this);">
<input type="button" value="Something" onclick="window.location.href='www.google.com';">

If what you're really trying to do is update some data structure then maybe something more like this (assumes your data object has a "server" property that is an array of url objects, like you seemed to be trying to do in your original function):如果你真正想做的是更新一些数据结构,那么可能更像这样(假设你的数据 object 有一个“服务器”属性,它是一个 url 对象的数组,就像你在原来的功能):

var myData = {
   "server": [ {"position": "center",
                "url": "New_Url"   } ]
};

function addUrl(newUrl) {
   myData["server"].push{"position":"center",
                         "url" : newUrl});
}
function updateUrl(index,newUrl) {
   myData["server"][index]["url"] = newUrl;
} 

Been a month since I used Javascript, but here it goes... I found a website that might be able to help you out.自从我使用 Javascript 以来已经一个月了,但是就这样……我找到了一个网站,也许可以帮助你。 It appears older, but works.它看起来较旧,但有效。

The link is here: http://www.htmlgoodies.com/tutorials/buttons/article.php/3478871/So-You-Want-A-Link-Button-Huh.htm链接在这里: http://www.htmlgoodies.com/tutorials/buttons/article.php/3478871/So-You-Want-A-Link-Button-Huh.htm

This is what he suggests:这是他的建议:

    <FORM>
    <INPUT TYPE="button" onClick="parent.location='page.html'">
    </FORM>

Just change out the "page.html" with the URL of the page you want to click and go to.只需将“page.html”更改为您要单击的页面的 URL 和 go 即可。 I won't get into the concept of JavaScript here, but if you're willing to learn more, see my JavaScript primers.我不会在这里讨论 JavaScript 的概念,但如果您愿意了解更多信息,请参阅我的 JavaScript 引物。

Hope this helps.希望这可以帮助。

Try this尝试这个

 <script type="text/javascript">

    function addurl(newUrl){
    "server": [
                 {
                    "position": "center",
                    "url": newUrl
                 }
              ]
                     }
    </script>

 <input type="button" onclick="addUrl();" value="url123">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM