简体   繁体   English

更改功能参数onclick?

[英]Changing function parameters onclick?

 <div class='unsubscribe'><a id='us$id' href='#' onclick='subscribe(u,$id);'>
<img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>

onclick how do i change the first parameter in the onclick function to 's'? onclick如何将onclick函数中的第一个参数更改为“ s”? So the next time it will look like this. 因此,下一次它将如下所示。

<div class='unsubscribe'><a id='us$id' href='#' onclick='subscribe(s,$id);'>
<img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>

I wouldn't do it like you want to. 我不会像你想要的那样做。 See the XY Problem . 请参阅XY问题

Instead, what you should so, is keep track of the subscription state of the user, either using a cookie or an identifier on the link ( data-state="s" ), and take notes in the function. 相反,您应该这样做,即使用cookie或链接上的标识符( data-state="s" )来跟踪用户的订阅状态,并在函数中记笔记。

Instead of setting onclick in your HTML do it with Javascript soon after. 不久后,您无需在HTML中设置onclick,而是使用Javascript来实现。 I'm assuming you're echoing all your code as a double quoted PHP string because of 'us$id' : 我假设您因为'us$id'而将所有代码作为双引号PHP字符串回显:

<div class='unsubscribe'>
    <a id='us$id' href='#'>
        <img src='/unsubscribe.jpg' alt='unsubscribe' />
    </a>
</div>
<script type='text/javascript'>
    document.getElementById('us$id').onclick = function(){
        // Subscribe u
        subscribe(u, $id);

        // Set all future clicks to subscribe s
        this.onclick = function(){
            subscribe(s, $id);
        };
    };
</script>

Instead of changing the string in the onclick , you can change it in the javascript itself. 除了在onclick中更改字符串外,您还可以在javascript本身中更改它。 This is if you want to change all of the s to u throughout the page (wasn't sure if there was only one or if this is what your intention is). 这是如果要在整个页面中将所有s更改为u(不确定是否只有一个,或者这是否是您的意图)。

Remove the first parameter: 删除第一个参数:

<div class='unsubscribe'><a id='us$id' href='#' onclick='subscribe($id);'>
<img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>

Then change subscribe() to this: 然后将subscribe()更改为此:

function subscribe(id)
{
    // doing "static" variable in javascript
    if (typeof this.foo == 'undefined')
    {
        this.foo = 's';
    }
    else
    {
        this.foo = 'u';
    }

    ...
}

One way would be to change it textually; 一种方法是按文本进行更改; use var el = document.getElementById('us$id'); 使用var el = document.getElementById('us$id'); to retrieve the element, then search el.attributes for an attribute with .name value "onclick", and replace its child with a new TextNode with the second function call. 检索元素,然后在el.attributes中搜索具有.name值“ onclick”的属性,并使用第二个函数调用用新的TextNode替换其子级。

A different approach would be to change the HTML so that subscribe will get both u and s, and somehow (eg with a global variable, a static variable, etc.) remember if it's the first or second time that this method is invoked. 另一种方法是更改​​HTML,以便订阅将同时获取u和s,并以某种方式(例如,使用全局变量,静态变量等)记住该方法是第一次还是第二次被调用。

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

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