简体   繁体   English

CSS-js渲染链接无用

[英]CSS - js rendering links useless

Ok, well, thanks to people here, I figured out the portion of my js code that's rendering my links useless is what's pasted below... Still trying to figure out why, but if someone has any insight, that'd be great... 好的,好了,感谢这里的人们,我想出了我的js代码中使我的链接变得无用的部分是下面粘贴的内容...仍然试图弄清楚为什么,但是如果有人有任何见识,那就太好了。 ..

function initJumpMenus() {
    // Turns all <select> elements with the 'jumpmenu' class into jump menus
    var selectElements = document.getElementsByTagName("select");
    for( i = 0; i < selectElements.length; i++ ) {
        // Check for the class and make sure the element has an ID
        if( selectElements[i].className == "jumpmenu" &&
            document.getElementById(selectElements[i].id) != ""
        ) {
            jumpmenu = document.getElementById(selectElements[i].id);
            jumpmenu.onchange = function() {
                if( this.options[this.selectedIndex].value != '' ) {
                    // Redirect
            location.href=this.options[this.selectedIndex].value;
                }
            }
        }
    }
}

window.onload = function() {
    initJumpMenus();
}

This: 这个:

document.getElementById(selectElements[i].id) != ""

is wrong. 是错的。 You want to check if the element has an id, so simply do: 您要检查元素是否具有ID,只需执行以下操作:

selectElements[i].id != ""

I'd like to point out that you can improve your code here and there: 我想指出的是,您可以在这里和那里改进代码:

1 Don't write for loops that get the length property for each iteration, 1不要编写for获取每次迭代的length属性的循环,

instead do: 而是:

for( i = 0, num = selectElements.length; i < num; i++ ) {
    ...
}

Only if you expect selectElements to grow or shrink it would make sense to requery the property value, but in that case, you probably should not write a for loop anyway. 仅当您希望selectElements增大或缩小时,才可以重新查询属性值,但是在那种情况下,您可能无论如何都不应该编写for循环。

2: Don't write nodelist[index] 2:不要写nodelist[index]

For nodelists, such as returned by getElementsByTagName() you should not write nodelist[index] (even though most browser support that). 对于节点列表,例如getElementsByTagName()返回的节点,您不应编写nodelist[index] (即使大多数浏览器都支持该列表)。 The standard DOM method is item , so write nodelist.item(index) instead. 标准的DOM方法是item ,所以改写nodelist.item(index)

3 fetch items from the list only once 3仅一次从列表中提取项目

If you need an item from the list more than once, store it in a local variable. 如果您需要多次从列表中选择一项,请将其存储在局部变量中。 your loop would become: 您的循环将变为:

for( i = 0, selectElement; i < selectElements.length; i++ ) {
    selectElement = selectElements.item(i);
    ...more code using selectElement...
}

Note the declaration of the selectElement variable in the for loop. 请注意for循环中selectElement变量的声明。 since you don't use it outside the loop, declaring it there avoids clutter and ensures that if you move the loop, you move the declaration. 由于不在循环之外使用它,因此在其中声明它可以避免混乱,并确保如果移动循环,则可以移动声明。

4. cheapest comparisons first 4.最便宜的比较

You wrote: 你写了:

selectElement.className == "jumpmenu" &&
selectElement.id != ""

this could be slightly improved if you reverse the legs: 如果您将双腿倒转,可以稍微改善一下:

selectElement.id != "" &&
selectElement.className == "jumpmenu"

This will be faster, since it is less work to check if a string is empty, and for those cases where the string is empty, we won't even check the className 这样会更快,因为检查字符串是否为空的工作量较小,对于那些字符串为空的情况,我们甚至不会检查className

5 Don't use document.getElementById() if you already have the element 5如果您已有元素,请不要使用document.getElementById()

Inside the loop you have this: 在循环内部,您有以下内容:

jumpmenu = document.getElementById(selectElements[i].id);

You are basically getting the id from the selectElement and use that to query the document to find ....the element having and id equal to that of the current selectElement . 您基本上是从selectElement获取ID的,并使用该ID来查询文档以查找.... id等于当前selectElement Becuase id 's are unique within the document (or should be), you are basically writing a completely unecessary sentence. 因为id在文档中是唯一的(或应该是),所以您基本上在写一个完全不必要的句子。 jumpmenu and selectElement refer to one and the same object. jumpmenuselectElement引用一个相同的对象。

6. onchange handler improvements 6. onchange处理程序的改进

inside the loop you assign an onchange handler. 在循环内,您分配了一个onchange处理程序。 you do so by creating a new function for each loop iteration. 为此,您需要为每个循环迭代创建一个新函数。 This is the handler code: 这是处理程序代码:

function() {
    if( this.options[this.selectedIndex].value != '' ) {
        // Redirect
        location.href=this.options[this.selectedIndex].value;
    }
}

Three things are of note here: First, the onchange handler code contains this location.href = ... that should probably be document.location.href = ... . 这里要注意三件事:首先,onchange处理程序代码包含此location.href = ...可能应该是document.location.href = ...

Second, you refer twice to this.options[this.selectedIndex].value . 其次,您两次引用this.options[this.selectedIndex].value Again, put this in a local variable. 同样,将其放在局部变量中。

Third, the this refers to the element that experienced the onchange event by the time this function is executing. 第三, this是指在执行此功能时经历过onchange事件的元素。 Other than this and properties of this , there are no variables in this handler that originate from the loop or the outer initJumpMenus function. 除了thisthis属性之外, this处理程序中没有任何源自循环或外部initJumpMenus函数的变量。 You should simply create it once, outside the loop, and assign it every iteration: 您只需在循环外部创建一次,并在每次迭代中分配它:

var onchange_handler = function() {
    if( this.options[this.selectedIndex].value != "" ) {
        // Redirect
        location.href=this.options[this.selectedIndex].value;
    }
}
for (...) {
    if (...) {
        selectElement.onchange = onchange_handler;
    }
}

7 Summary 7小结

Putting it all toghether, this is how I would write it: 综上所述,这就是我的写法:

function initJumpMenus() {
    var handler = function() {
        var value = this.options.item(this.selectedIndex).value;
        if( value != "" ) {
            // Redirect
            document.location.href = value;
        }
    }
    var selectElements = document.getElementsByTagName("select");
    for(var i = 0, num = selectElements.length, selectElement; i < num; i++ ) {
        selectElement = selectElements.item(i);
        // Check for the class and make sure the element has an ID
        if( selectElement.id != "" &&
            selectElement.className == "jumpmenu"
        ) {
            selectElement.onchange = handler;
        }
    }
}

Check the error console; 检查错误控制台; any exception in the JS will stop the links form reacting to a click. JS中的任何异常都会停止链接表单对点击的反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM