账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    select如何选中option后自动跳转到对应的网页?
    80
    0

    大家好!
    我自己用html5编了一个程序。想实现的功能是通过选择select下的option,选中后,网页主体位置显示跳转到相对应的网页,但由于本人编程经验不足,还想请教各位大侠如何实现。源代码如下,
    其中同目录下已经放置了包含了主文件及test1.html,test2.html,test3.html共计4个文件

    <!DOCTYPE html>
    
    <html>
    <head>
        <meta charset="UTF-8"/>
        <title>select触发链接</title>
        
    </head>
    
    <body style="margin: 0">
        <table style="background: DarkGray;width: 100%;height: 890px;">
            
                <tr style="background: Cornsilk">
    
                    <td colspan="3" style="height: 10%;text-align:center;margin:50px 0; font:normal 56px/56px 'MicroSoft YaHei'">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</td>
    
                </tr>
    
                <tr>
                    <td style="background: Cornsilk; width:12%; text-align:center;margin:50px 0; font:normal 24px/24px 'MicroSoft YaHei';color:#ffffff">
                        
                        <form action="" >
    
                            <select name='test' >
                                  <option value='test1'>网页1</option>
                                  <option value='test2'>网页2</option>
                                  <option value='test3'>网页3</option>
                            </select>    
                        </form>
    
                    </td>
    
                    <td  colspan="2">
                        
                        <iframe 
                        src="test1.html"; width="100%"; height="100%" name="test"
                        
                        ></iframe>
                        
                    </td>
    
    
                </tr>
    
                <tr style="background: Cornsilk"><td colspan="3" style="height: 2%"></td>
                </tr>
            
        </table>
    
    </body>
    </html>

    非常感谢!

    3
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • ①颗薪為誰而空 普通会员 1楼

      在JavaScript中,你可以使用addEventListener方法来为option元素添加一个点击事件监听器,然后在监听器的回调函数中设置一个变量来表示选中的选项。以下是一个简单的示例:

      ```javascript // 获取所有的option元素 var options = document.getElementsByTagName('option');

      // 遍历所有的option元素 for (var i = 0; i < options.length; i++) { // 在每个option元素上添加一个点击事件监听器 options[i].addEventListener('click', function() { // 设置一个变量来表示选中的选项 var selectedOption = options[i]; // 跳转到对应的网页 window.location.href = selectedOption.value; }); } ```

      在这个示例中,当用户点击一个选项时,会触发一个点击事件监听器。在监听器的回调函数中,会获取当前选中的选项,然后使用window.location.href属性来跳转到对应的网页。

      请注意,这只是一个基本的示例,你可能需要根据你的具体需求来修改这个代码。例如,你可能需要在用户点击选项后显示一个警告消息,或者在跳转到新的网页后更新其他页面的内容等。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部