脚本标记在体内工作但在外部不起作用

时间:2014-02-10 13:00:11

标签: javascript jquery script-tag

您好我创建了一个使用模态显示输出的网页,我进入了一个场景,我将脚本标签放入正文并且程序运行结束,但是当我清理我的代码并放入脚本标签时在身体外面。一些部分停止运行,如果你可以帮助我调试它,我的代码会很感激。 这是一个小提琴:http://jsfiddle.net/6x63w/

<div class="infobox">
    <div id="shade"></div>
    <div id="modal">
        <div id="envholder">

            <img src="placeholder.png" id="placeholderImg" style="display: none;" />
            <input type="button" value="Close" id='close2'>
        </div>
    </div> <span class="infohead"><a href="http://ojt.cloudstaff.com/Project9/" id="find-post-code">Address Format</a></span>

    <div id="WC"><span class="infoques">Which Country?</span>

        <form action="" method="get">
            <select name="code" id="code">
                <option value="Aland Islands">Åland Islands</option>
                <option value="Albania">Albania</option>
                <option value="American Samoa">American Samoa</option>
                <option value="Andorra">Andorra</option>
                <option value="Antigua and Barbuda">Antigua and Barbuda</option>
                <option value="Argentina">Argentina</option>
                <option value="Armenia">Armenia</option>
                <option value="Australia">Australia</option>
                <option value="Austria">Austria</option>
                <option value="Azerbaijan">Azerbaijan</option>
                <option value="Belarus">Belarus</option>
                <option value="Belgium">Belgium</option>
                <option value="Bermuda">Bermuda</option>
                <option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
                <option value="Brunei Darussalam">Brunei Darussalam</option>
                <option value="Bulgaria">Bulgaria</option>
                <option value="Canada">Canada</option>
                <option value="Cayman Islands">Cayman Islands</option>
                <option value="Chile">Chile</option>
                <option value="China">China</option>
                <option value="Croatia">Croatia</option>
                <option value="Cyprus">Cyprus</option>
                <option value="Czech Republic">Czech Republic</option>
                <option value="Denmark">Denmark</option>
                <option value="Dominican Republic">Dominican Republic</option>
                <option value="Ecuador">Ecuador</option>
                <option value="Estonia">Estonia</option>
                <option value="Faroe Islands">Faroe Islands</option>
                <option value="Finland">Finland</option>
                <option value="France">France</option>
                <option value="French Guiana">French Guiana</option>
                <option value="French Polynesia">French Polynesia</option>
                <option value="Germany">Germany</option>
                <option value="Greece">Greece</option>
                <option value="Greenland">Greenland</option>
                <option value="Guadeloupe">Guadeloupe</option>
                <option value="Guam">Guam</option>
                <option value="Guatemala">Guatemala</option>
                <option value="Holy See">Holy See</option>
                <option value="Hong Kong">Hong Kong</option>
                <option value="Hungary">Hungary</option>
                <option value="Iceland">Iceland</option>
                <option value="India">India</option>
                <option value="Ireland">Ireland</option>
                <option value="Isle of Man">Isle of Man</option>
                <option value="Italy">Italy</option>
                <option value="Jamaica">Jamaica</option>
                <option value="Japan">Japan</option>
                <option value="Jordan">Jordan</option>
                <option value="Kazakhstan">Kazakhstan</option>
                <option value="Kenya">Kenya</option>
                <option value="Latvia">Latvia</option>
                <option value="Lesotho">Lesotho</option>
                <option value="Liechtenstein">Liechtenstein</option>
                <option value="Lithuania">Lithuania</option>
                <option value="Luxembourg">Luxembourg</option>
                <option value="Macedonia, The Former Yugoslav Republic of">Macedonia</option>
                <option value="Madagascar">Madagascar</option>
                <option value="Malaysia">Malaysia</option>
                <option value="Maldives">Maldives</option>
                <option value="Marshall Islands">Marshall Islands</option>
                <option value="Martinique">Martinique</option>
                <option value="Mayotte">Mayotte</option>
                <option value="Mexico">Mexico</option>
                <option value="Micronesia, Federated States of">Micronesia</option>
                <option value="Moldova">Moldova</option>
                <option value="Monaco">Monaco</option>
                <option value="Mozambique">Mozambique</option>
                <option value="Nauru">Nauru</option>
                <option value="Nepal">Nepal</option>
                <option value="Netherlands">Netherlands</option>
                <option value="New Caledonia">New Caledonia</option>
                <option value="New Zealand">New Zealand</option>
                <option value="Norfolk Island">Norfolk Island</option>
                <option value="Northern Mariana Islands">Northern Mariana Islands</option>
                <option value="Norway">Norway</option>
                <option value="Pakistan">Pakistan</option>
                <option value="Palau">Palau</option>
                <option value="Panama">Panama</option>
                <option value="Papua New Guinea">Papua New Guinea</option>
                <option value="Paraguay">Paraguay</option>
                <option value="Philippines">Philippines</option>
                <option value="Pitcairn">Pitcairn</option>
                <option value="Poland">Poland</option>
                <option value="Portugal">Portugal</option>
                <option value="Puerto Rico">Puerto Rico</option>
                <option value="Reunion">Reunion</option>
                <option value="Romania">Romania</option>
                <option value="Russian Federation">Russian Federation</option>
                <option value="Saint Barthelemy">Saint Barthelemy</option>
                <option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option>
                <option value="Saint Martin">Saint Martin</option>
                <option value="San Marino">San Marino</option>
                <option value="Singapore">Singapore</option>
                <option value="Slovakia">Slovakia</option>
                <option value="Slovenia">Slovenia</option>
                <option value="South Africa">South Africa</option>
                <option value="Spain">Spain</option>
                <option value="Sri Lanka">Sri Lanka</option>
                <option value="Svalbard and Jan Mayen">Svalbard and Jan Mayen</option>
                <option value="Swaziland">Swaziland</option>
                <option value="Sweden">Sweden</option>
                <option value="Switzerland">Switzerland</option>
                <option value="Turkey">Turkey</option>
                <option value="Tuvalu">Tuvalu</option>
                <option value="Uganda">Uganda</option>
                <option value="Ukraine">Ukraine</option>
                <option value="United Arab Emirates">United Arab Emirates</option>
                <option value="United Kingdom">United Kingdom</option>
                <option value="United States">United States</option>
                <option value="Uruguay">Uruguay</option>
                <option value="Venezuela">Venezuela</option>
                <option value="Virgin Islands, U.S.">Virgin Islands, U.S.</option>
                <option value="Wallis and Futuna">Wallis and Futuna</option>
            </select>
            <input type="button" id="start" value="Show Address Format" class="button">
    </div>
    <script type="text/javascript">
        var modal = document.getElementById('modal');
        var shade = document.getElementById('shade');
        document.getElementById('start').onclick = function() {
            var q = document.getElementById("code");
            var selected = q.options[q.selectedIndex].value;

            var src = "img2/" + selected + ".jpg";
            var img = document.getElementById("placeholderImg");
            img.src = src;
            img.style.display = "inline";
            modal.style.display = shade.style.display = 'block';
        };

        document.getElementById('close2').onclick = function() {
            modal.style.display = shade.style.display = 'none';
        };

    </script>
    </form>
</div>

2 个答案:

答案 0 :(得分:4)

尝试在$(document).ready()中为您编写代码,以便DOM可以使用,否则可能无法使用

试试吧,

$(function(){
   // your js code
});

答案 1 :(得分:0)

只需将脚本标记留在正文中,一切都会正常工作。

如果要清理HTML文件,请将您的Javascript代码放入外部JS文件中,然后将其加载到正文末尾:

    <script type="text/javascript" src="main.js"></script>
</body>