【问题标题】:Resolving a Javascript conflict解决 Javascript 冲突
【发布时间】:2012-02-10 19:52:06
【问题描述】:

我和 JS 有冲突。我为网站上的选择元素编写了两个脚本。一种用于在没有提交按钮的情况下提交它,另一种用于自定义样式选择。以下是其中两个:

 <script type="text/javascript"><!--
                var dropdown = document.getElementById("stallions");
                function onStallion() {
                  if ( dropdown.options[dropdown.selectedIndex].value != null ) {
                    location.href = dropdown.options[dropdown.selectedIndex].value;
                  }
                }
                dropdown.onchange = onStallion;
            --></script>

  // Jquery plugin for styling selectboxes from DressCode.

$(document).ready(function(){
// first locate all of the select tags on the page and hide them
$("select#stallions").css('display','none');
//now, for each select box, run this function
$("select#stallions").each(function(){

    var curSel = $(this);
    // get the CSS width from the original select box
    var gddWidth = $(curSel).css('width');
    var gddWidthL = gddWidth.slice(0,-2);
    var gddWidth2 = gddWidthL - 28;
    var gddWidth3 = gddWidthL - 16;
    // build the new div structure
    var gddTop = '<div style="width:' + gddWidthL + 'px" class="selectME" tabindex="0"><div class="cornerstop"><div><div></div></div></div><div class="middle"><div><div><div>';
    //get the default selected option
    var whatSelected = $(curSel).children('option:selected').text();
    //write the default
    var gddFirst = '<div class="first"><span class="selectME gselected" style="width:'+ gddWidth2 +  'px;">'+ whatSelected +'</span><span id="arrowImg"></span><div class="clears"></div></div><ul class="selectME">';
    // create a new array of div options from the original's options
    var addItems = new Array();      
    $(curSel).children('option').each( function() {           
        var text = $(this).text();  
        var selVal = $(this).attr('value'); 
        var before =  '<li style="width:' + gddWidthL + 'px;"><a href="#" rel="' + selVal + '" tabindex="0"  style="width:' + gddWidth3 + 'px;">';
        var after = '</a></li>';           
        addItems.push(before + text + after);
    });
    //hide the default from the list of options 
    var removeFirst = addItems.shift();
    // create the end of the div selectbox and close everything off
    var gddBottom ='</ul></div></div></div></div><div class="cornersbottom"><div><div></div></div></div></div>'
    //write everything after each selectbox
    var GDD = gddTop + gddFirst + addItems.join('') + gddBottom;
    $(curSel).after(GDD);
    //this var selects the div select box directly after each of the origials
    var nGDD = $(curSel).next('div.selectME');

    $(nGDD).find('li:first').addClass("first");

    $(nGDD).find('li:last').addClass('last');
    //handle the on click functions - push results back to old text box
    $(nGDD).click( function(e) {
         var myTarA = $(e.target);
         var myTarT = $(e.target).text();
         var myTar = $(e.target);
         //if closed, then open
         if( $(nGDD).find('li').css('display') == 'none')
            {
                    //this next line closes any other selectboxes that might be open
                    $('div.selectME').find('li').css('display','none');
                    $(nGDD).find('li').css('display','block');

                    //if user clicks off of the div select box, then shut the whole thing down
                    $(document.window || 'body').click( function(f) {
                            var myTar2 = $(f.target);
                            if (myTar2 !== nGDD) {$(nGDD).find('li').css('display','none');}
                    });
                            return false;
            }
            else
            {      
                    if (myTarA == null){
                        $(nGDD).find('li').css('display','none');
                                return false;
                            }
                            else {
                                //set the value of the old select box
                                $(curSel).val(myTarA);
                                //set the text of the new one
                                 $(nGDD).find('span.gselected').text(myTarT);
                                 $(nGDD).find('li').css('display','none');
                                 return false;
                            }
            }
    //handle the tab index functions
     }).focus( function(e) {        


         $(nGDD).find('li:first').addClass('currentDD');
         $(nGDD).find('li:last').addClass('lastDD');
         function checkKey(e){
            //on keypress handle functions
            function moveDown() {
                var current = $(nGDD).find('.currentDD:first');
                var next = $(nGDD).find('.currentDD').next();
                if ($(current).is('.lastDD')){
                return false;
                } else {
                    $(next).addClass('currentDD');
                    $(current).removeClass('currentDD');
                }
            }
            function moveUp() {
                var current = $(nGDD).find('.currentDD:first');
                var prev = $(nGDD).find('.currentDD').prev();
                if ($(current).is('.first')){
                return false;
                } else {
                    $(prev).addClass('currentDD');
                    $(current).removeClass('currentDD');
                }
            }
            var curText = $(nGDD).find('.currentDD:first').text();
            var curVal = $(nGDD).find('.currentDD:first a').attr('rel');
           switch (e.keyCode) {
                case 40:
                    $(curSel).val(curVal);
                    $(nGDD).find('span.gselected').text(curText);
                    moveDown();
                    return false;
                    break;
                case 38:
                    $(curSel).val(curVal);
                    $(nGDD).find('span.gselected').text(curText);
                    moveUp();
                    return false;
                    break;
                case 13:
                    $(nGDD).find('li').css('display','none');
                    }     
        }
        $(document).keydown(checkKey);  
    }).blur( function() {
            $(document).unbind('keydown');
    });
});
});

第一个嵌入在页面的 php 文件中,第二个是从外部 js 文件中提取的。我测试了它,我知道这是他们两个之间的冲突。如果我使用第二个脚本,则要提交的第一个脚本不起作用。如果我删除第二个脚本,它工作得很好。

我很乐意就导致错误的原因以及如何解决它获得任何帮助。感谢您的帮助!

【问题讨论】:

    标签: javascript conflict


    【解决方案1】:

    问题在于 DressDown 代码与下拉菜单交互的方式。

    你有这行的地方(第 69 行):

    //set the value of the old select box
    $(curSel).val(myTarA);
    

    它不会触发列表的 onchange。您可以简单地在该行之后立即添加 location.href,这应该可以满足您的需求:

    ....
    //set the value of the old select box
    $(curSel).val(myTarA);
    location.href = myTarA;
    //set the text of the new one
    ....
    

    【讨论】:

    • 我试过了,但它只会在选择时再次加载相同的页面。它应该重定向到适当的链接。
    • 我还尝试根据第一个代码需要发生的情况在其中使用它:location.href = dropdown.options[dropdown.selectedIndex].value;但它不起作用,它只是重新加载同一页面。
    • 您是否尝试在代码中添加警报(myTarA)以查看正在传递的值?由于我不知道您的下拉菜单中的内容,因此很难在某个时间点之后进行故障排除。
    • 我实际上在 WP 使用它。这是选择选项代码:&lt;select id="stallions"&gt; &lt;option value=""&gt;--&lt;?php _e('Select a stallion', 'tullstorp')?&gt;--&lt;/option&gt; &lt;?php while ( $stallions-&gt;have_posts() ) : $stallions-&gt;the_post(); ?&gt; &lt;option class="stallion" id="&lt;?php the_ID() ?&gt;" value="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title() ?&gt;&lt;/option&gt; &lt;?php endwhile; ?&gt; &lt;/select&gt;
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    相关资源
    最近更新 更多