我正在构建一个将 svg 图标呈现到 Shadow Dom 页面上的 Web 组件。IE
<ba-icon i="airplane"></ba-icon>
Run Code Online (Sandbox Code Playgroud)
我有一个外部 svg sprite 文件,其中包含大量 SVG 图标。
Web 组件的内部将以下内容渲染到 Shadow Dom 中:
<svg>
<use xlink:href="i.dist.svg#i-airplane"></use>
<svg>
Run Code Online (Sandbox Code Playgroud)
一切都在屏幕上正确呈现,但我想要一些嵌入在 SVG 内部的信息,特别是包含在viewbox, 中的信息(例如:)viewBox="0 0 32 32"。
我知道在 中渲染的内容use也输入到 Shadow Dom 中。但我试图找到另一种获取嵌入到use. 我试图对 svg 的内容进行 ajax,但这对于页面上的多个图标来说变成了一个大问题,因为 Web 组件的每个实例现在都在进行该调用。我还能怎么做?
以供参考:
我正在寻找一种方法来测试浏览器是否可以在use元素中有一个外部svg引用:
<svg>
<use xlink:href="sprite.svg#icon-name"></use>
</svg>
Run Code Online (Sandbox Code Playgroud)
这适用于大多数现代浏览器,但不适用于IE11或Edge等浏览器.而不是测试浏览器"名称"是否为"x",我宁愿使用"功能检测"来知道我是否需要运行回退.
我无法访问use的Shadow DOM以查看是否在其中放置了外部引用,并且我认为我无法检测"内容"是否实际放置在引用外部svg的临时元素中.
例如,我无法以这种方式设置元素:
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
var use = document.createElementNS('http://www.w3.org/2000/svg', 'use');
use.setAttributeNS(
'http://www.w3.org/1999/xlink',
'xlink:href',
'sprite.svg#icon-name'
);
svg.appendChild(use);
document.body.appendChild(svg);
// Somehow test to see if it worked...
document.body.appendChild(svg);
Run Code Online (Sandbox Code Playgroud)
实际上有一种方法可以检测到这一点,还是我必须检测浏览器"名称"?
当两个元素都在使用时,我们如何让Flexbox停止在兄弟元素中均衡空间flex-grow: 1.这很难在前面解释,因此这里的代码会快速跟随问题的示例屏幕截图和所需的行为.
.Parent {
display: flex;
flex-direction: column;
background-color: lightcoral;
width: 400px;
min-height: 200px;
}
.Parent>div {
flex: 1;
}
.child1 {
background-color: lightblue;
}
.child2 {
background-color: lightgreen;
}Run Code Online (Sandbox Code Playgroud)
<div class="Parent">
<div class="child1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sagittis lorem at odio euismod tincidunt. Proin aliquet velit nec augue venenatis laoreet. Etiam nec metus mi. Aliquam sit amet velit non lectus porttitor accumsan sit amet egestas risus.</div>
<div class="child2">Lorem ipsum</div>
</div>Run Code Online (Sandbox Code Playgroud)
我正在尝试仅使用标准导入语法将类的静态成员导入到文件中。给出上下文:
class Person {
static walk() {
console.log('Walking');
}
}
let {walk} = Person;
console.log(walk); // walk function
Run Code Online (Sandbox Code Playgroud)
然而,我认为导入的行为就像解构赋值。如果这是真的,那么我希望以下内容能够发挥作用。但是,当我尝试导入 walk 方法时,它只是返回为undefined:
person.js
export default class Person {
static walk() {
console.log('Walking');
}
}
Run Code Online (Sandbox Code Playgroud)
walker.js
import {walk} from './person';
console.log(walk); // undefined
Run Code Online (Sandbox Code Playgroud)
由于这似乎不起作用,如何将静态方法从类导入到另一个模块?
注意:这似乎是 Chrome 独有的问题
onMouseEnter当阻塞事件元素的元素消失时,React 目前不会触发该事件。标准 JS 事件甚至委托事件都不是这种情况。
这是我遇到的问题的简化描述和代码示例;当下部元素的工具提示消失(并且光标位于上部元素上方)时,onMouseEvent不会触发:
function reactMouseEnter({currentTarget}) {
console.log('React Mouse Enter Event');
const rect = currentTarget.getBoundingClientRect();
const tooltip = document.createElement('div');
tooltip.classList.add('tooltip');
tooltip.style.top = rect.y - 30 + 'px';
document.body.appendChild(tooltip);
const mouseLeaveHandler = () => {
currentTarget.removeEventListener('mouseleave', mouseLeaveHandler);
setTimeout(() => {
tooltip.parentNode.removeChild(tooltip);
}, 300);
};
currentTarget.addEventListener('mouseleave', mouseLeaveHandler);
}
class Example extends React.Component {
ref = React.createRef()
componentDidMount() {
this.ref.current.addEventListener('mouseenter', () => {
console.log('Regular Mouse Enter Event');
})
}
render() {
return (
<div>
<div onMouseEnter={ …Run Code Online (Sandbox Code Playgroud)我试图使用flexbox而不是浮点数在两个内容之间创建省略号.我需要的效果可能永远不会与浮动使用.
第一个"列"包含的信息不如第二列中的重要信息,因此应该在第二列中的任何内容之前截断.
这种效果在Chrome和FireFox中运行良好,但在Safari中失败了.
我根据caniuse使用了正确版本的Safari ,它显示了对flexbox的完全支持.
对于实时版本,以及我想要实现的精简版本:
.parent {
background-color: lightgrey;
padding: 10px 0;
display: flex;
max-width: 410px;
margin-bottom: 2px;
}
.less-important {
background-color: lightpink;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 0 5px;
}
.more-important {
background-color: lightgreen;
white-space: nowrap;
padding: 0 5px;
}Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="less-important">Truncate this text because it is less important</div>
<div class="more-important">The important text</div>
</div>
<div class="parent">
<div class="less-important">No ellipsis</div>
<div class="more-important">Important stuff</div>
</div>
<div class="parent">
<div class="less-important">Less important content</div>
<div class="more-important">Way more …Run Code Online (Sandbox Code Playgroud)我想实现以下目标:
认为x = 321.31125
现在我想要第二位小数点后的 ceil 值......所以预期结果应该是321.32
注意:我知道四舍五入会返回 321.31,但我想要 321.32
我不明白为什么在某些计算中可以使用空数组或只有1"数字"值的数组.
[] * [] === 0 //true
[2] * [2] === 4 //true
["2"] * ["2"] === 4 //true
Run Code Online (Sandbox Code Playgroud)
但是,对于每个操作员来说似乎并非总是如此.
[2] + [1] === 3 // false, actual result is "21"
[2] - [1] === 1 // true
Run Code Online (Sandbox Code Playgroud)
我也检查了这些,并看到了预期的结果:
[4] === "4" // false
[4] === 4 // false
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,我希望NaN在任何这些数学运算中.什么是JavaScript数组允许这个?是因为数组toString方法在某种程度上是在内部使用的吗?
我在 DocumentFragment 内部工作,并试图将 HTML 插入其中。我们知道 DocumentFragment 没有innerHTML setter,所以我尝试在DocumentFragment 中插入一个临时节点并在它之后使用 .html 插入HTML insertAdjacentHTML。然后我会删除临时元素,并留下一个带有我想要的 DOM 的片段。
示例:(错误是故意的)
var fragment = document.createDocumentFragment();
var temporaryElement = fragment.appendChild(document.createElement('div'));
// Before we move on.. I can see that the `temporaryElement` does have a parent
console.log(temporaryElement.parentNode.nodeType);
// The following throws an error...
temporaryElement.insertAdjacentHTML('afterend', '<div>Some crazy custom HTML..</div>');
fragment.removeChild(temporaryElement);Run Code Online (Sandbox Code Playgroud)
我可以清楚地看到临时元素有一个 parentNode,那么为什么会出现这个错误呢?
我正在尝试打印这个数据数组.我的数据如下:
[[["Not Critical","Not Critical"]],[["Not Critical","Not Critical"]],[["Not Critical","Not Critical"]]]
Run Code Online (Sandbox Code Playgroud)
我需要像这样打印:(这意味着每个数组中的数据必须在下一行),
[
[
[
"Not Critical",
"Not Critical"
]
],
[
[
"Not Critical",
"Not Critical"
]
],
[
[
"Not Critical",
"Not Critical"
]
]
]
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
[
[
["Not Critical", "Not Critical"]
],
[
["Not Critical", "Not Critical"]
],
[
["Not Critical", "Not Critical"]
]
]
Run Code Online (Sandbox Code Playgroud)
注意:我不想在下一行打印每个数组,而不是那样,数组内的数据必须在下一行打印.
javascript ×8
html ×3
arrays ×2
css ×2
flexbox ×2
svg ×2
angularjs ×1
babeljs ×1
ecmascript-6 ×1
html-imports ×1
jquery ×1
reactjs ×1
safari ×1