在JavaScript中,是以下代码:
window.onresize = function() {
// Do something.
}
Run Code Online (Sandbox Code Playgroud)
同样如下:
$(window).on('resize', function () {
// Do something.
});
Run Code Online (Sandbox Code Playgroud)
这两个代码块在功能方面是否相同?使用其中一个是否有任何优点或缺点(无论多么微小)?
关于什么:
window.addEventListener('resize', function(event) {
// Do something.
});
Run Code Online (Sandbox Code Playgroud) 使用 Puppeteer,我选择了一些 HTML 元素:
await page.$$( 'span.styleNumber' );
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法获取元素的文本:
console.log( await ( await styleNumber.getProperty( 'innerText' ) ).jsonValue() );
Run Code Online (Sandbox Code Playgroud)
如何获取元素的 data-Color 属性的值?
这是我的脚本:
<span class="styleNumber" data-Color="Blue">SG1000</span>
<span class="styleNumber" data-Color="Green">SG2000</span>
<span class="styleNumber" data-Color="Red">SG3000</span>
Run Code Online (Sandbox Code Playgroud)
const puppeteer = require( 'puppeteer' );
( async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto( 'http://www.example.com/sample.php' );
// Get a list of all elements.
var styleNumbers = await page.$$( 'span.styleNumber' );
// Print the style numbers.
for( let styleNumber of …Run Code Online (Sandbox Code Playgroud) 我为什么MutationObserver没有检测到使用textContent完成的文本更改而感到头疼.
<div id="mainContainer">
<h1>Heading</h1>
<p>Paragraph.</p>
</div>
Run Code Online (Sandbox Code Playgroud)
function mutate(mutations) {
mutations.forEach(function(mutation) {
alert(mutation.type);
});
}
jQuery(document).ready(function() {
setTimeout(function() {
document.querySelector('div#mainContainer > p').textContent = 'Some other text.';
}, 2000);
var target = document.querySelector('div#mainContainer > p')
var observer = new MutationObserver( mutate );
var config = { characterData: true, attributes: false, childList: false, subtree: true };
observer.observe(target, config);
});
Run Code Online (Sandbox Code Playgroud)
在上面的脚本中,段落元素的文本内容明显改变,但MutationObserver没有检测到它.
但是,如果将textContent更改为innerHTML,则会提示您"characterData"已更改.
为什么MutationObserver检测innerHTML但不检测textContent?
这是JS小提琴:
https://jsfiddle.net/0vp8t8x7/
请注意,如果将textContent更改为innerHTML,则只会收到警报.
我使用以下命令通过 Laravel Mix 模块捆绑我的脚本:
npm run dev // Compile scripts.
npm run prod // Compile and minify scripts.
Run Code Online (Sandbox Code Playgroud)
这些是原生 npm 命令还是自定义 Laravel Mix 命令?它们在哪里定义?
我注意到它们在 Laravel package.json 中被列为“脚本”。这些脚本到底是什么,通过 Laravel Mix 为 Webpack 自定义命令?
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --disable-host-check --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
Run Code Online (Sandbox Code Playgroud) 我正在尝试填充数组方法 includes() 以与 IE8 一起使用,我需要支持一个项目并且我不想使用 indexOf()。
我知道有一个 polyfill,所以我去了:
并将其包含在我的脚本顶部。
IE8 对 Object.defineProperty() 的支持有限,所以我也对它进行了 polyfill:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill
最后,我需要 polyfill Object.keys():https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
放在一起,我的 polyfills 是:
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
throw new …Run Code Online (Sandbox Code Playgroud) 当您查看源代码时,我的 HTML 如下所示:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是在我这样做之后:
$dom = new DOMDocument();
$dom->loadHTML($html);
$dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)
我的源代码变成了这样:
<!DOCTYPE html><html><head></head><body></body></html>
Run Code Online (Sandbox Code Playgroud)
使用 PHP DOMDocument() 类及其方法时如何保留新行和空格?
如何断开我的变异观察者与其回调函数的连接?正如他们应该观察到的变化,但我想在第一次变化后断开观察者.由于观察者变量超出范围,因此它不会断开连接.如何将observer变量传递给回调函数,以便代码可以工作?
function mutate(mutations) {
mutations.forEach(function(mutation) {
if ( mutation.type === 'characterData' ) {
console.log('1st change.');
observer.disconnect(); // Should disconnect here but observer variable is not defined.
}
else if ( mutation.type === 'childList' ) {
console.log('2nd change. This should not trigger after being disconnected.');
}
});
}
jQuery(document).ready(function() {
setTimeout(function() {
document.querySelector('div#mainContainer p').innerHTML = 'Some other text.';
}, 2000);
setTimeout(function() {
jQuery('div#mainContainer').append('<div class="insertedDiv">New div!<//div>');
}, 4000);
var targetOne = document.querySelector('div#mainContainer');
var observer = new MutationObserver( mutate );
var config = …Run Code Online (Sandbox Code Playgroud)如何仅使用 JavaScript 而不是 jQuery 以编程方式触发焦点事件?
例如,在下面的代码中,其想法是应该提醒“Hello, world!” 因为 focusout() 函数(或类似的事件引发函数)被调用(focusout() 不是 JS 函数,但就是这个想法)。
function helloWorld () {
alert('Hello, world!');
}
document.getElementsByTagName( 'form' )[0].addEventListener( 'focusout', function( eventObj ) {
helloWorld();
});
var event = new Event('focusout');
document.getElementsByTagName( 'form' )[0].dispatchEvent(event); Run Code Online (Sandbox Code Playgroud)
<form action="" method="post" id="sampleForm">
<input type="text" id="linkURL" name="linkURL" placeholder="Link URL"><br>
<input type="submit" name="action" value="Submit">
</form>Run Code Online (Sandbox Code Playgroud)
我在 CentOS 7 上安装了 MariaDB 10.2.10,但它停止运行。如果我做:
如果我做:
systemctl restart mariadb.service
Run Code Online (Sandbox Code Playgroud)
我得到:
mariadb.service: main process exited, code=killed, status=6/ABRT
Failed to start MariaDB 10.2.12 database server.
Unit mariadb.service entered failed state.
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它运行了一年多,现在突然无法启动。这是一个生产服务器,所以我有几个我需要的数据库;我不能从头开始重新安装它。
我该怎么做才能在不丢失数据库的情况下恢复并运行 MariaDB?
systemd: mariadb.service: main process exited, code=killed, status=6/ABRT
Asystemd: Failed to start MariaDB 10.2.12 database server.
Asystemd: Unit mariadb.service entered failed state.
Asystemd: mariadb.service failed.
systemd: mariadb.service holdoff time over, scheduling restart.
systemd: Starting MariaDB 10.2.12 database server...
mysqld: 2018-04-24 7:06:38 140101542774912 [Note] /usr/sbin/mysqld (mysqld 10.2.12-MariaDB) starting as …Run Code Online (Sandbox Code Playgroud) 使用 Windows 命令提示符或 Windows PowerShell,如何将单个目录中的所有文件名输出到文本文件,而不带文件扩展名?
在命令提示符中,我使用的是:
dir /b > files.txt
Run Code Online (Sandbox Code Playgroud)
01 - Prologue.mp3
02 - Title.mp3
03 - End.mp3
files.txt
Run Code Online (Sandbox Code Playgroud)
01 - Prologue
02 - Title
03 - End
Run Code Online (Sandbox Code Playgroud)
请注意,“dir /b > files.txt”命令包含文件扩展名并将文件名放在底部。
在不使用批处理文件的情况下,是否有一个干净的命令提示符或 PowerShell 命令可以完成我正在寻找的任务?
javascript ×6
jquery ×2
node.js ×2
windows ×2
blur ×1
centos7 ×1
disconnect ×1
domdocument ×1
ecmascript-6 ×1
focusout ×1
html ×1
laravel ×1
laravel-mix ×1
linux ×1
mariadb ×1
mysql ×1
newline ×1
node-modules ×1
npm ×1
php ×1
polyfills ×1
powershell ×1
puppeteer ×1
resize ×1