如何访问 D3 回调中的当前选择?
group.selectAll('.text')
.data(data)
.enter()
.append('text')
.text((d) => d)
.attr('class', 'tick')
.attr('y', (d) => {
// console.log(this) <-- this gives me window :( but I want the current selection or node: <text>
return d
})
Run Code Online (Sandbox Code Playgroud)
我可以d3.select('.tick')在回调中做一个,因为到那时我已经添加了一个类并且可以通过 d3.select 获取节点,但是如果我没有添加这个类呢?
如何在Clojure中返回给定字符串的有效URL.
(re-matches #"????" "www.example.com"))
(re-matches #"????" "http://example.com"))
(re-matches #"????" "http://example.org")) // returns "http://example.org"
(re-matches #"????" "htasdtp:/something")) // returns nil
Run Code Online (Sandbox Code Playgroud) 为了更好地理解递归,我决定尝试使用递归函数来查找链表中的项而不是while循环.
接近结束时,我的递归函数似乎是通过返回正确的节点做正确的事情,但然后它运行意外返回几次(我不知道为什么)并返回一个不正确的对象.我错过了什么吗?
var linkedList = {
element: 'head',
next: {
element: 'SF',
next: {
element: 'LA',
next: {
element: 'SD',
next: {
element: 'NY',
next: null
}
}
}
}
}
function getItem(city, list) {
var item = list
if (item.element != city) {
item = item.next
getItem(city, item)
}
return item
}
console.log( getItem('SD', linkedList ) ) // logs "SF" node but I expect "SD" node
Run Code Online (Sandbox Code Playgroud) 我是CasperJS的新手.怎么this.echo(this.getTitle());工作但console.log("Page Title ", document.title);不是?另外为什么我的document.querySelector不工作?有没有人有一个很好的解释?我可以在CasperJS文档中找到答案吗?
这是我的代码:
var casper = require('casper').create();
var url = 'http://www.example.com/';
casper.start(url, function() {
this.echo(this.getTitle()); // works
this.echo(this.getCurrentUrl()); // works
});
casper.then(function(){
this.echo(this.getCurrentUrl()); // works
console.log("this is URL: ", document.URL); // doesn't work
console.log("Page Title ", document.title); // doesn't work
var paragraph = document.querySelectorAll('p')[0].innerHTML;
console.log(paragraph); // doesn't work
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
编辑: 我现在正在使用casper.thenEvaluate和casper.evaluate,它仍然无法正常工作.有任何想法吗?
var casper = require('casper').create();
var url = 'http://www.example.com/';
casper.start(url, function() {
this.echo(this.getTitle()); // works
this.echo(this.getCurrentUrl()); // works
console.log('page loaded: '); // works …Run Code Online (Sandbox Code Playgroud) javascript ×3
casperjs ×1
clojure ×1
d3.js ×1
function ×1
linked-list ×1
recursion ×1
regex ×1