简体   繁体   English

Javascript onClick-替换URL中的变量值

[英]Javascript onClick - replace variable value in URL

I'm hoping this is a quick one but I seem to be having a mental block today. 我希望这是一个快速的方法,但是今天我似乎有一个精神障碍。

I have a series of divs which when clicked, I'm wanting to replace a value at the end of my URL. 我有一系列的div,当单击这些div时,我想替换URL末尾的值。

URL example would be - http://www.test.com/_CMS/edit-product.php?divClick=1 网址示例为-http: //www.test.com/_CMS/edit-product.php?divClick=1

The value I'm wanting to change as the user clicks each div would be divClick=" assigned value to the div " 我要在用户单击每个div时更改的值将为divClick =“ 为div分配值

Hope this makes sense and thanks in advance. 希望这是有道理的,并预先感谢。

Set an attribute such an id for each div 为每个div设置一个属性,例如一个id

<div id="1" onClick="MyFunction(this.id)"> </div>

    <script>
    function Myfunction(clickedId)
    {

         window.location="http://www.test.com/_CMS/edit-product.php?  divClick="+clickedId;

    }
    </script>

i supposed that you wanted to be redirected to some url when a div is clicked so i added window.location, but you should have said what you wanted to do. 我以为您想在单击div时希望重定向到某些url,所以我添加了window.location,但您应该已经说过要做什么。

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

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