替换匹配函数给出了未定义的错误,但它与替换一起工作
Replace Match Function is giving undefined error, but it works with replace
我有链接,它们应该滚动到正确的部分,但是,除非我首先更改它们的id,否则主主题标题将不起作用。一旦我这么做了,它就不再滚动了。
这里有一个网站链接(必须将浏览器设置为780px以下)http://lookupblue.com/elclimo,请注意,如果您选择婚礼之类的副标题,整个页面将滚动,但如果您选择活动,则不会滚动。这是因为id#Events正被其他东西使用,所以在滚动到该位置之前,我需要将#Events替换为#Section1。
我目前的错误是,无法找到未定义的顶部,这让我相信匹配或替换不起作用。
<script type="text/javascript">
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent)) {
function backupNavigation_click(clicked_href)
{
var abc = clicked_href.substring(clicked_href.lastIndexOf('/') +
1);
if (abc.match("^#")) {
$('a').on("click", function(e) {
// prevent default state change
e.preventDefault();
// get href of clicked
var abc = $(this).attr('href');
// Find last '/' and get everything after
abc = abc.substring(abc.lastIndexOf('/') + 1,
abc.length);
// If '#' found then chop off
if (abc.indexOf('#') > -1) {
abc = abc.substring(1, abc.length);
}
var mapObj = {
Home:"Section0",
Events:"Section1",
Vehicles:"Section2",
Testimonials:"Section3",
Specials:"Section4",
AboutUs:"Section5",
Quotes:"Section6"
};
abc = abc.replace(/Home|Events|Vehicles|Testimonials|Specials|AboutUs|Quotes/gi, function(matched){
return mapObj[matched];
});
console.log(abc);
// Find matching with matching id
var pos = $('#' + abc).offset().top;
console.log(pos);
// Scroll to it
$('html, body').animate({
scrollTop: pos
}, 400);
})
} else {
$('a').on("click", function(e) {
// prevent default state change
e.preventDefault();
// get href of clicked
var abc = $(this).attr('href');
// Find last '/' and get everything after
abc = abc.substring(abc.lastIndexOf('/') + 1,
abc.length);
// If '#' found then chop off
if (abc.indexOf('#') > -1) {
abc = abc.substring(1, abc.length);
}
console.log(abc);
// Find matching with matching id
var pos = $('#' + abc).offset().top;
console.log(pos);
// Scroll to it
$('html, body').animate({
scrollTop: pos
}, 400);
})
}
}
} else {
if (window.innerWidth > 779) {
$('head').append(
' <script type="text/javascript" src="js/jquery.fullPage.js">'
);
}
$(document).ready(function() {
$('#fullpage').fullpage({
anchors: ['Home', 'Events', 'Vehicles',
'Testimonials', 'Specials',
'AboutUs', 'Quotes'
],
sectionsColor: ['none', 'none', 'none',
'none', 'none', 'none', 'none'
],
scrollOverflow: true
});
});
}
</script>
工作的替换代码看起来像:
var str = "I have a cat, a dog, and a goat.";
var mapObj = {
cat:"dog",
dog:"goat",
goat:"cat"
};
str = str.replace(/cat|dog|goat/gi, function(matched){
return mapObj[matched];
});
alert(str);
我得到了它,只需要在做子字符串之前做替换函数。
相关文章:
- 要替换的Javascript Regex无法按预期工作
- 为什么这个脚本只工作一次?只替换元素一次,然后再也不替换
- 替换匹配函数给出了未定义的错误,但它与替换一起工作
- 在表单提交将DOM的一部分替换为分部之后,我应该将ajax成功绑定到什么来使我的javascript工作
- 字符串替换不'当通过android webView JavascriptInterface传递字符串时无法工作
- 立即工作并将所有英文数字替换为阿拉伯数字的函数
- 全局替换不起作用,但简单的替换工作正常
- JavaScript中的字符串替换无法正常工作
- 如何使替换代码工作
- 正则表达式替换为多个通配符在 PHP 中工作,而不是在 JavaScript 中
- 替换仅工作一次
- 似乎无法获得示例代码来替换复选框工作
- Javascript替换在控制台中工作,而不是在代码中
- Java脚本:替换未按预期工作的函数
- 简化工作函数以替换特定单词
- 确认不工作时替换上载的文档
- 这个RegEx和替换在JavaScript中究竟是如何工作的
- Jquery换行符替换为br标签不工作
- Javascript替换-不与html字符串工作
- 替换按钮不能工作