Can';我想不出是什么';JavaScript中的if语句有问题[更新9月1日-我的for循环运行了两次,如何修复?]
Can';我想不出是什么';JavaScript中的if语句有问题[更新9月1日-我的for循环运行了两次,如何修复?],javascript,html,for-loop,Javascript,Html,For Loop,var MarkUpChecker=(函数iffe(){ "严格使用",; 变量URL={ “foo”:http://foo.com', “酒吧”:http://bar.com', "baz":"http://baz.com', ‘耶’:’http://www.yay.com', “废话”:http://www.blargh.com' }, publicAPI; 函数getURL(){ for(var i=0;i
var MarkUpChecker=(函数iffe(){
"严格使用",;
变量URL={
“foo”:http://foo.com',
“酒吧”:http://bar.com',
"baz":"http://baz.com',
‘耶’:’http://www.yay.com',
“废话”:http://www.blargh.com'
},
publicAPI;
函数getURL(){
for(var i=0;i })();代码>已编辑强>
将id添加到锚点
并尝试使用jQuery
更改
标记目标属性:
<a id="custom_target" href="http://foo.com/path/to_page.aspx">
<img src="img/some_image.jpg">
</a>
if (link.href.indexOf(url) !== -1) {
$('#custom_target').attr('target', '_self');
}
else{
$('#custom_target').attr('target', '_blank');
}
代码中存在多个问题,包括逻辑问题。比如说
function getURL() {
for (var i = 0; i < arguments.length; i++) {
return URLS[arguments[i]];//always returns arguments[0]
}
}
函数getURL(){
for(var i=0;i
我重写它的方式是,它接受并返回数组(参见下面的JSFIDLE)。
下一期
//root problem. this for loop See solution in JSFiffle
for (var i = 0, len = arguments.length; i < len; i++) { // Using a for loop to allow unlimited arguments to be passed in
url = getURL(arguments[i]); // We are calling the publicApi.getURL() method and passing the looped argument from above
//and rewrite target again and again. I remove outer loop
for (var j = 0, jlen = avon_rep_container_links.length; j < jlen; j++) { // This loop goes over the whole documents links...
link = avon_rep_container_links[j];
if (link.href.indexOf(url) !== -1) { //...and we are checking each argument passed in to see if it matches the object value stored in the getURL function e.g. like a switch statement..
parser = document.createElement('a'); //...if so we are essentially adding a blank tag to all the documents in the document
parser.href = link.href;//parser isn't used
link.setAttribute('target', '_self');//will be changed in the next loop
newPathName = parser.pathname;
if (newPathName.search(/Executive|District|Division|National/) != -1) { // Added check for these strings for SMO page
newPathName = newPathName.split('/').pop();
newstr = newPathName;
} else {
newstr = newPathName;
}
link.href = newstr;
} else {
link.setAttribute('target', '_blank');
}
}
//根本问题。此for循环请参见JSFiffle中的解决方案
for(var i=0,len=arguments.length;i