将字符串插入按钮onclick location.href Javascript

时间:2017-03-14 07:33:52

标签: javascript jquery

我有一个这样的按钮:

<button id="sync"  type="button" onclick="location.href = 'http://localhost/manage/test_yeezy/yooo/massSync/key/23ds31231ada/';" data-ui-id="sync-button">
    <span>Sync All</span>
</button>

我想在 massSync / 之后插入一个字符串 / listing_id / 45 / ,这个网址可以是动态的,有没有办法做到这一点? / p>

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式完成此操作......

var link = 'http://localhost/manage/test_yeezy/yooo/massSync/key/23ds31231ada/';
var newLink = link.replace(/(massSync)/, '$1/listing_id/45');
$('#sync').attr("onclick", "location.href = '" + newLink + "'");
var location_href = $('#sync').attr('onclick').replace('location.href = ', '');
console.log(location_href);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="sync"  type="button" onclick="location.href = 'http://localhost/manage/test_yeezy/yooo/massSync/key/23ds31231ada/';" data-ui-id="sync-button">
    <span>Sync All</span>
</button>

** 假设massSync是静态的