我的许多脚本如下所示:
if (...) {
const myvariable1 = document.querySelector('.class-1');
const myvariable2 = document.querySelector('.class-2');
function someFunction() {
// Do something with myvariable1 or myvariable2
}
someFunction();
}
Run Code Online (Sandbox Code Playgroud)
它们在 Chrome、Firefox、Edge 和 Opera 上运行良好,但在 Safari 上我收到错误:
ReferenceError: Can't find variable myvariable1
Run Code Online (Sandbox Code Playgroud)
解决方法
如果我在 if 语句之前声明常量,代码就会工作......
const myvariable1 = document.querySelector('.class-1');
const myvariable2 = document.querySelector('.class-2');
if (...) {
function someFunction() {
// Do something with myvariable1 or myvariable2
}
someFunction();
}
Run Code Online (Sandbox Code Playgroud)
...但我不明白为什么,也不知道如何使常量在全球范围内可用。
也许有人可以向我解释 Safari 独有的行为。
如何使 :not()-selector 在以下示例中起作用?
HTML
<div id="content">
<div id="one">
<div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
<div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
<div id="two">
<div class="mystyle"><p>This is a <a href="">link</a>.</p></div> <!-- should NOT be green -->
<div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
#content a:not(.mystyle) {
color: green;
}
Run Code Online (Sandbox Code Playgroud)
JSFIDDLE
重要的
我正在尝试在我的博客上设置外部小部件的样式。我无权访问它的 HTML,因此更改 HTML 不是一种选择。我正在寻找仅使用 CSS 的解决方案。
在手册中,我找到了 pandoc lua-filter 的示例:
return {
{
Str = function (elem)
if elem.text == "{{helloworld}}" then
return pandoc.Emph {pandoc.Str "Hello, World"}
else
return elem
end
end,
}
}
Run Code Online (Sandbox Code Playgroud)
我想替换{{helloworld}}为<div>abc</div>. 我的尝试:
return {
{
Str = function (elem)
if elem.text == "{{helloworld}}" then
return pandoc.RawInline('html','<div>abc</div>')
else
return elem
end
end,
}
}
Run Code Online (Sandbox Code Playgroud)
...但这给了我以下输出:
<p></p>
<div>abc</div>
<p></p>
Run Code Online (Sandbox Code Playgroud)
我怎样才能摆脱空p标签?
附加信息
我从 markdown 转换为 html,我的 markdown 文件如下所示:
我使用 UTM 参数来跟踪 Google Analytics 中的传入链接。
假设我的 URL 看起来像这样
https://www.domain.tld/shop?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale
Run Code Online (Sandbox Code Playgroud)
我想清理 URL。想要的结果是
https://www.domain.tld/shop
Run Code Online (Sandbox Code Playgroud)
在网上我找到了以下片段
(function() {
var win = window;
var removeUtms = function(){
var location = win.location;
if (location.search.indexOf('utm_') != -1 && history.replaceState) {
history.replaceState({}, '', window.location.toString().replace(/(\&|\?)utm([_a-z0-9=]+)/g, ""));
}
};
ga('send', 'pageview', { 'hitCallback': removeUtms });
})();
Run Code Online (Sandbox Code Playgroud)
目前,我的 CMS 中的 Google Analytics 模板如下所示
<?php
/**
* To use this script, please fill in your Google Analytics ID below
*/
$GoogleAnalyticsId = 'UA-bla-bla';
/**
* DO NOT EDIT ANYTHING …Run Code Online (Sandbox Code Playgroud)