有没有类似于此代码$(';#date1';,this.el)[0]的纯Javascript?
我有一个应用程序,我用主干网和木偶网构建,我想这是一个jQuery函数或类似的东西,我在木偶网视图中找到了这段代码有没有类似于此代码$(';#date1';,this.el)[0]的纯Javascript?,javascript,backbone.js,marionette,Javascript,Backbone.js,Marionette,我有一个应用程序,我用主干网和木偶网构建,我想这是一个jQuery函数或类似的东西,我在木偶网视图中找到了这段代码 $('#publicdate',this.el)[0] 在纯Javascript中是否有类似的代码?我试过这个密码 document.getElementById('date1') this.getElementById('date1') 但不起作用 这里是完整的代码: programming.module("Program.Chart", function(Chart, pr
$('#publicdate',this.el)[0]
在纯Javascript中是否有类似的代码?我试过这个密码
document.getElementById('date1')
this.getElementById('date1')
但不起作用
这里是完整的代码:
programming.module("Program.Chart", function(Chart, programming, Backbone, Marionette, $, _){
Chart.chartT = Marionette.ItemView.extend({
template : "#row",
tagName : "tr"
})
Chart.chartV = Marionette.CompositeView.extend({
childView : Chart.chartT,
childViewContainer : "tbody#detail",
template : "#chart",
onRender : function(){
//DatePicker Range
var
startDate,
endDate,
updateStartDate = function() {
startPicker.setStartRange(startDate);
endPicker.setStartRange(startDate);
endPicker.setMinDate(startDate);
},
updateEndDate = function() {
startPicker.setEndRange(endDate);
startPicker.setMaxDate(endDate);
endPicker.setEndRange(endDate);
},
startPicker = new Pikaday({
field: $('#date1',this.el)[0],
minDate: new Date(),
maxDate: new Date(2020, 12, 31),
onSelect: function() {
startDate = this.getDate();
updateStartDate();
}
}),
endPicker = new Pikaday({
field: $('#date2',this.el)[0],
minDate: new Date(),
maxDate: new Date(2020, 12, 31),
onSelect: function() {
endDate = this.getDate();
updateEndDate();
}
}),
_startDate = startPicker.getDate(),
_endDate = endPicker.getDate();
if (_startDate) {
startDate = _startDate;
updateStartDate();
}
if (_endDate) {
endDate = _endDate;
updateEndDate();
}
var selectdate = $('#publicdate',this.el)[0];
selectdate.addEventListener("change",function(){
alert("Changed")
})
//Chart JS
var dataChart = programming.request("data:entities");
console.log(dataChart.models)
var labels = ['12/08/2016','13/08/2016','16/08/2016']
var series = [[100,210,311],[49,10,7]]
var data = {
labels : labels,
series : series
}
var option = {
showArea : true,
lineSmooth : false,
chartPadding : {
bottom:30,
top:30
},
axisX : {
showGrid:false
},
axisY : {
},
plugins : [
Chartist.plugins.ctAxisTitle({
axisX: {
axisTitle: 'Tanggal',
axisClass: 'ct-axis-title',
offset: {
x: 0,
y: 50
},
textAnchor: 'middle'
},
axisY: {
axisTitle: 'Jumlah Penjualan',
axisClass: 'ct-axis-title',
offset: {
x: 0,
y: 0
},
textAnchor: 'middle',
flipTitle: false
}
}),
Chartist.plugins.ctPointLabels({
textAnchor : "middle"
})
]
}
new Chartist.Line($('.statistic',this.el)[0],data,option)
}
})
Chart.notfound = Marionette.ItemView.extend({
template : "#notfound"
})
})
提前谢谢
在纯Javascript中是否有类似的代码
这是纯JavaScript。但是,如果您想使用DOM而不是jQuery来执行此操作,可以使用querySelector
:
this.el.querySelector('#publicdate')
…假设this.el
是一个HTML元素
querySelector
在document
和单个元素上都可用。当您在元素上使用它时,它只在该元素的树中显示。它接受任何有效的CSS选择器,并返回对第一个匹配元素的引用,或null
。因此,上面的操作与jQuery代码所做的完全相同(除了在未找到时为您提供null
而不是undefined
):仅当id=“publicdate”
元素存在于this.el
的树中时,才查找该元素
还有querySelectorAll
,它返回匹配元素的列表。所有现代浏览器和IE8都支持这两种浏览器
我试过这个密码
document.getElementById('date1')
但不起作用
嗯,
date1
和publicdate
是不同的ID。但是document.getElementById(“publicdate”)
和this.el.querySelector(“#publicdate”)
之间的最大区别是,后者只会在this.el
的子代树中找到元素,然而,getElementById
将在文档中的任何位置找到它。getElementById应该可以工作。也许你用错身份证了。