前端 | iframe框架标签应用(三)| 点击指定部分,进行外部页面搜索,内置iframe返回搜索结果

📚实现效果

  • 点击单词列表内的任意单词↓
    在这里插入图片描述
  • 弹出对应单词的搜狗翻译搜索结果,点击关闭按钮关闭界面。
    在这里插入图片描述

📚模块实现解析

  • 列表框搜索功能的基础上加一个click触发效果就好了,也就是说,在对应js文件里加上下述内容↓

    function openSouGouSearch(word) {
        const existingIframe = document.querySelector('iframe');
        const existingCloseButton = document.querySelector('.close-button');
    
        if (existingIframe) {
            document.body.removeChild(existingIframe);
        }
        if (existingCloseButton) {
            document.body.removeChild(existingCloseButton);
        }
    
        const iframe = document.createElement('iframe');
        iframe.src = `https://fanyi.sogou.com/text?keyword=${encodeURIComponent(word)}`;
        iframe.style.position = 'fixed';
        iframe.style.top = '10%';
        iframe.style.left = '10%';
        iframe.style.width = '80%';
        iframe.style.height = '85%';
        iframe.style.border = '2px solid #bfc1a9';
        iframe.style.zIndex = '9999';
        iframe.style.borderRadius = '10px'; 
        document.body.appendChild(iframe);
    
        // 添加关闭按钮
        const closeButton = document.createElement('button');
        closeButton.textContent = '×';
        closeButton.classList.add('close-button');
        closeButton.style.position = 'fixed';
        closeButton.style.width = '20px';
        closeButton.style.height = '20px';
        closeButton.style.top = '8%';
        closeButton.style.right = '8%';
        closeButton.style.zIndex = '10000';
        closeButton.style.border = '1.2px solid #bfc1a9';
        closeButton.style.borderRadius = '50%'; 
        closeButton.style.fontFamily = 'serif';
        closeButton.style.fontSize = '15px';
        closeButton.style.color = 'white';
        closeButton.style.fontWeight = 'bold';
        closeButton.style.padding = '2px';
        closeButton.style.backgroundColor = '#d24735';
        closeButton.addEventListener('click', () => {
            document.body.removeChild(iframe);
            document.body.removeChild(closeButton);
        });
        document.body.appendChild(closeButton);
    }
    
    // 点击搜索结果触发打开搜狗搜索功能
    document.getElementById('searchList').addEventListener('click', function(event) {
        const clickedElement = event.target;
        if (clickedElement.tagName === 'LI') {
            const word = clickedElement.innerHTML.split(' - ')[1];
            openSouGouSearch(word);
        }
    });
    
    • 解析点击操作对应的目标单词: const word = clickedElement.innerHTML.split(' - ')[1];
    • 访问对应链接,其中encodeURIComponent()word进行编码转换,以将其用作URL的一部分,使其能够在网络上进行安全传输。
      iframe.src = `https://fanyi.sogou.com/text?keyword=${encodeURIComponent(word)}`;
      
    • 整体的iframe框架其实就是喵喵大王立大功里白噪音喇叭页面导入的套用。
    • 排除连续点击单词多个iframe框架堆叠问题(也就是说如果没点关闭就又点击了新的单词,旧单词对应的搜索页面自动关闭)
      if (existingIframe) {
          document.body.removeChild(existingIframe);
      }
      if (existingCloseButton) {
          document.body.removeChild(existingCloseButton);
      }
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啦啦右一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值