当前位置: 首页 > news >正文

购物网站的搜索框用代码怎么做精品课程 网站建设质量

购物网站的搜索框用代码怎么做,精品课程 网站建设质量,网站建设项目的生命周期,内部网站搭建官方网站: Web Component - Web API 接口参考 | MDN 1. 给一个html文件的路径字符串path, 存储对应path下的template,script,style数据 1) 传入path 2) 使用fetch将path字符串所在的文件找到并返回内容 const res await fetch(path).then(res > res.text()); 3) 使用…

官方网站:

Web Component - Web API 接口参考 | MDN

1. 给一个html文件的路径字符串path, 存储对应path下的template,script,style数据

1) 传入path

2) 使用fetch将path字符串所在的文件找到并返回内容

const res = await fetch(path).then(res => res.text());

3) 使用DOMParser实例的parseFromString方法将至转换为shadowRoot

const parser = new DOMParser();
const doc = parser.parseFromString(res, "text/html"); // shadowRoot

4) shadowRoot支持dom查找元素的方法, 可以找到template, script, style标签元素

5) 根据不同的path, 存放相应的template, script, style

// 动态加载组件并解析
async function loadComponent(path, name) {this.caches = this.caches || {};// 当缓存存在if (!!this.caches[path]) {return this.caches[path]}const res = await fetch(path).then(res => res.text());// 利用DOMParser,生成shaow-rootconst parser = new DOMParser();const doc = parser.parseFromString(res, "text/html");// 解析模板,脚本,样式const template = doc.querySelector("template");const script = doc.querySelector("script");const style = doc.querySelector("style");// 缓存内容this.caches[path] = {template,script,style}return this.caches[path]
}

2. custom-component.js(自定义标签custom-component)

// 容器组件
class CustomComponent extends HTMLElement {async connectedCallback() {console.log("custom-component connected");// 获取组件的path,即html的路径const strPath = this.getAttribute("path");// 加载htmlconst cInfos = await loadComponent(strPath);// Element.attachShadow() 方法给指定的元素挂载一个 Shadow DOM,并且返回对 ShadowRoot 的引用。const shadow = this.attachShadow({ mode: "closed" });// 添加html对应的标签和内容this._addElements(shadow, cInfos);}_addElements(shadow, info) {if (info.template) {shadow.appendChild(info.template.content.cloneNode(true));}if (info.script) {var fn = new Function(`${info.script.textContent}`);// 绑定脚本的this为当前的影子根节点, 防止全局污染fn.call(shadow);}if (info.style) {shadow.appendChild(info.style);}}
}
window.customElements.define("custom-component", CustomComponent);

使用自定义标签custom-component

// 使用示例: renderComponent.call(thisArg, route)
function renderComponent(route) {var el = document.createElement("custom-component");el.setAttribute("path", `/${route.component}.html`);// append可以追加节点和字符串,appendChild只能追加节点;el.id = "_route_";this.append(el);
}

1) 先新建goods.html

<template><div>商品详情-goos</div><div class="product-id">下面商品是????</div>
</template><script>alert('这是商品页!!!')
</script><style>.product-id{color: red;}
</style>

2) 在index.html使用

先引入custom-component.js

然后在index.html的script标签写:

renderComponent.call(document.getElementById('box'), {component: './goods'})

index.html文件代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>index</title><style></style>
</head><body><div id="box"></div>
</body><script src="./custom-component.js"></script><script>renderComponent.call(document.getElementById('box'), {component: './goods'})</script>
</html>

http://www.yayakq.cn/news/202808/

相关文章:

  • 河北建设银行石家庄分行招聘网站国家超算互联网公司排名
  • 有没有专门做图的网站在线制作电子公章免费公章在线生成
  • 潍坊高端网站建设郑州医疗网站开发
  • html编写新闻页面seo课程培训要多少钱
  • ppt做视频 模板下载网站网站建设技术服务合同
  • 大连网站建设短期培训班公司企业邮箱号
  • 个人网站用什么域名好小游戏开发
  • 克拉玛依网站建设百度发布
  • 策划咨询昆明网站seo报价
  • 怎么知道网站的空间是谁做的51网站空间相册在哪里
  • wordpress login 出错天津做网站优化哪家好
  • 久久建筑网图集下载个人seo优化
  • 什么样的公司开做网站网站管理登录
  • mysql做网站怎么查看数据库wordpress+高性能
  • 建设摩托车公司官方网站公司网站需要多少钱
  • 电脑配件网站建设玉田做网站
  • 曲阜做网站哪家好做网站开发需要什么
  • 小米网站建设书企业账号登录入口
  • 上海华亮建设集团网站杭州百度推广代理商
  • joomla 2.5:你的网站建设_使用与管理网站建设于朦胧
  • 做网站需要具备的基础条件系统网站推广方案怎么做
  • 做网站郑州公司企业手机网站建设咨询
  • 青岛的网站建设公司h5技术网站
  • 用安卓做网站wordpress mu 下载
  • 最新网站备案有空间与域名后怎么做网站
  • 免费站长工具wordpress 阿里云 漏洞
  • 公司网站怎么写阳江网约车
  • 美食网站开发报告关联词有哪些 全部
  • 在哪里可以免费做个人网站网业大全
  • 用ps做网站的网页框架湘潭网站建设定制磐石网络