[英]Javascript Update href of <a> tag on page load
I've written a code that looks like this.我写了一个看起来像这样的代码。 I should update every href of the tags that have the url I want to change.
我应该更新具有我想要更改的 url 的标签的每个 href。
<script>
var url = window.location.href;
myArr = url.split("?");
myArr[0] = "mynewurl";
new_url = myArr.join("#");
$(document).ready(function(){
$(".item a[href='myoldurl']").prop("href", new_url);
});
</script>
Unfortunately the href is always the same.不幸的是,href 总是相同的。 Here is the button in which my tag is contained.
这是包含我的标签的按钮。
<div class="de elBTN elAlign_center elMargin0 ui-droppable de-editable" id="tmp_button-57701" data-de-type="button" data-de-editing="false" data-title="button" data-ce="false" data-trigger="none" data-animate="fade" data-delay="500" style="margin-top: 30px; outline: none; cursor: pointer;" data-element-theme="customized" data-elbuttontype="1" aria-disabled="false">
<a href="https://pietroscataglini.typeform.com/to/c4dAZYjg" class="elButton elButtonSize1 elButtonColor1 elButtonRounded elButtonPadding2 elButtonFluid elButtonBlock elButtonCorner5 elBTNone elBtnVP_10 elBtnHP_20 elBTN_b_none elButtonShadow3 elButtonTxtColor1 deneg1pxLetterSpacing ea-buttonPulseGlow mfs_20" style="color: rgb(255, 255, 255); background: rgb(55, 149, 128); font-size: 20px;" rel="noopener noreferrer">
<span class="elButtonMain"><i class="fa_prepended fas fa-angle-double-right" contenteditable="false"></i>“Voglio diventare un Copywriter!"<i class="fa_appended fas fa-angle-double-left" contenteditable="false"></i></span>
<span class="elButtonSub mfs_14" style="font-size: 18px;">Compila il questionario per accedere alla consulenza gratuita introduttiva</span>
</a>
</div>
Here is the tag:这是标签:
<a href="myoldurl" class="elButton elButtonSize1 elButtonColor1 elButtonRounded elButtonPadding2 elButtonFluid elButtonBlock elButtonCorner5 elBTNone elBtnVP_10 elBtnHP_20 elBTN_b_none elButtonShadow3 elButtonTxtColor1 deneg1pxLetterSpacing ea-buttonPulseGlow mfs_20" style="color: rgb(255, 255, 255); background: rgb(55, 149, 128); font-size: 20px;" rel="noopener noreferrer">
<span class="elButtonMain"><i class="fa_prepended fas fa-angle-double-right" contenteditable="false"></i>“Voglio diventare un Copywriter!"<i class="fa_appended fas fa-angle-double-left" contenteditable="false"></i></span>
<span class="elButtonSub mfs_14" style="font-size: 18px;">Compila il questionario per accedere alla consulenza gratuita introduttiva</span>
</a>
You can do like this你可以这样做
let anc = document.getElementsByTagName('a');
for (const ancr of anc) {
if (ancr.href.includes('myoldurl')) {
const ahref = ancr.href.replace('myoldurl', 'mynewurl');
ancr.href = ahref;
}
}
You don't need to .item
class name in query selector because your <a>
element hasn't this class.您不需要在查询选择器中使用
.item
类名,因为您的<a>
元素没有此类。 Try this.试试这个。
btn.onclick = function changeUrl () { var newUrl = "https://pietroscataglini.typeform.com/to/c4dAZYjg"; /* var myArr = url.split("?"); myArr[0] = "mynewurl"; var new_url = myArr.join("#"); */ $("a[href='myoldurl']").prop("href", newUrl); $("#message").text(newUrl); }
#message { color: red }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="de elBTN elAlign_center elMargin0 ui-droppable de-editable" id="tmp_button-57701" data-de-type="button" data-de-editing="false" data-title="button" data-ce="false" data-trigger="none" data-animate="fade" data-delay="500" style="margin-top: 30px; outline: none; cursor: pointer;" data-element-theme="customized" data-elbuttontype="1" aria-disabled="false"> <a href="myoldurl" class="elButton elButtonSize1 elButtonColor1 elButtonRounded elButtonPadding2 elButtonFluid elButtonBlock elButtonCorner5 elBTNone elBtnVP_10 elBtnHP_20 elBTN_b_none elButtonShadow3 elButtonTxtColor1 deneg1pxLetterSpacing ea-buttonPulseGlow mfs_20" style="color: rgb(255, 255, 255); background: rgb(55, 149, 128); font-size: 20px;" rel="noopener noreferrer"> <span class="elButtonMain"><i class="fa_prepended fas fa-angle-double-right" contenteditable="false"></i>“Voglio diventare un Copywriter!"<i class="fa_appended fas fa-angle-double-left" contenteditable="false"></i></span> <span class="elButtonSub mfs_14" style="font-size: 18px;">Compila il questionario per accedere alla consulenza gratuita introduttiva</span> </a> </div> <br> Now <b>href</b> value is <span id="message">"myoldurl"</span>. <br>When you click on the button it will replace with your new url. <br> <input type="button" id="btn" value = "Change">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.