gpt4 book ai didi

javascript - JS在页面中进行字符串替换

转载 作者:行者123 更新时间:2023-12-03 10:46:00 30 4
gpt4 key购买 nike

我的代码中出现以下 HTML

<div id="optinforms-form1-name-field-container"> <input id="optinforms-form1-name-field" name="FNAME" placeholder="Vaše meno" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#666666" type="text"></div>

我想要一个js,它可以首先检查当前页面,如果访问了所需的页面,则它将上面代码中的字符串“Vaše meno”替换为“Your Name”。我已经尝试过以下代码,但由于我是 js 新手,我无法使其工作。我正在研究 wordpress 框架。

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if(sPage == "index.php"){
$("optinforms-form1-name-field").each(function() {
var text = $(this).text();
text = text.replace("Vaše meno", "Your Name");
$(this).text(text);});}

最佳答案

ID 选择器以“#”开头,您应该替换占位符的值,而不是文本。在 jQuery 中:

if (/index\.php$/i.test(window.location.pathname)) {
$('#optinforms-form1-name-field')[0].placeholder = 'Your name';

// or
// $('#optinforms-form1-name-field').prop('placeholder','Your name');
}

或者用普通的JS:

if (/index\.php$/i.test(window.location.pathname)) {
document.getElementById('optinforms-form1-name-field').placeholder = 'Your name';
}

关于javascript - JS在页面中进行字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570329/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com