Skip to Content
精通模块化JavaScript
book

精通模块化JavaScript

by Nicolás Bevacqua
April 2020
Intermediate to advanced
177 pages
1h 45m
Chinese
Publishing House of Electronics Industry
Content preview from 精通模块化JavaScript
4.2
 重构复杂代码
91
HTML
代码片段就可以在其他媒介中重复使用,比如不同的源站或者
PDF
文件,而不会破坏终端用户的体验。
下面的函数接收一个
HTML
片段,然后将
a[href
]
img[src]
通过类似
jQuery
DOM
工具库
$
转换为绝对路径
function absolutizeHtml(html, origin) {
const $dom = $(html)
$dom.find('a[href]').each(function () {
const $element = $(this)
const href = $element.attr('href')
$element.attr('href', absolute)
})
$dom.find('img[src]').each(function () {
const $element = $(this)
const src = $element.attr('src')
const absolute = absolutize(src, origin)
$element.attr('src', absolute)
})
return $dom.html()
}
因为这是一个短小的函数,所以这样编写
absolutizeHtml
是完全可以接受
的。然而,如果之后我们决定将
iframe[src]
script[src]
link[href]
加到我们希望转换路径的属性列表中时,可能会希望避免同时出现五份类 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
基于Python的智能文本分析

基于Python的智能文本分析

Benjamin Bengfort, Rebecca Bilbro, Tony Ojeda
Lua游戏AI开发指南

Lua游戏AI开发指南

Posts & Telecom Press, David Young

Publisher Resources

ISBN: 9787121387074