Javascript Wordpress 页面中的代码不是 运行
Javascript Code not running in Wordpress Pages
EDIT 这里是来自控制台的错误 https://imgur.com/a/OvP9cRr
我想 运行 在 jsfiddle 中工作的 Wordpress 页面中的一些代码。 https://jsfiddle.net/w9ta5x2e/。
我只是将我的代码放入编辑器中,但我什么也没看到。然而,脚本将 运行 如果它很简单,例如:由 https://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_inner_html
提供
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button>
</body>
</html>
我不知道如何创建 .js 文件,这是一个大学设置的 wordpress 站点,所以我怀疑我对 root 有多少控制权。
我不能在我的 Wordpress 网站上制作文件,它由一所大学管理,不允许我这样做
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script type="text/javascript">
jQuery(function() {
$.getJSON(
'https://cdn.jsdelivr.net/gh/highcharts/highcharts@v7.0.0/samples/data/usdeur.json',
function (data) {
Highcharts.chart('container', {
chart: {
zoomType: 'x'
},
title: {
text: 'USD to EUR exchange rate over time'
},
subtitle: {
text: document.ontouchstart === undefined ?
'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Exchange rate'
}
},
legend: {
enabled: false
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},
series: [{
type: 'area',
name: 'USD to EUR',
data: data
}]
});
}
);
});
</script>
Wordpress 通常在无冲突模式下运行 jQuery,这使得 $
未定义
尝试更改
jQuery(function() {// $ undefined here
到
jQuery(function($) { // $ is jQuery here
EDIT 这里是来自控制台的错误 https://imgur.com/a/OvP9cRr
我想 运行 在 jsfiddle 中工作的 Wordpress 页面中的一些代码。 https://jsfiddle.net/w9ta5x2e/。 我只是将我的代码放入编辑器中,但我什么也没看到。然而,脚本将 运行 如果它很简单,例如:由 https://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_inner_html
提供<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button>
</body>
</html>
我不知道如何创建 .js 文件,这是一个大学设置的 wordpress 站点,所以我怀疑我对 root 有多少控制权。
我不能在我的 Wordpress 网站上制作文件,它由一所大学管理,不允许我这样做
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script type="text/javascript">
jQuery(function() {
$.getJSON(
'https://cdn.jsdelivr.net/gh/highcharts/highcharts@v7.0.0/samples/data/usdeur.json',
function (data) {
Highcharts.chart('container', {
chart: {
zoomType: 'x'
},
title: {
text: 'USD to EUR exchange rate over time'
},
subtitle: {
text: document.ontouchstart === undefined ?
'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Exchange rate'
}
},
legend: {
enabled: false
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},
series: [{
type: 'area',
name: 'USD to EUR',
data: data
}]
});
}
);
});
</script>
Wordpress 通常在无冲突模式下运行 jQuery,这使得 $
未定义
尝试更改
jQuery(function() {// $ undefined here
到
jQuery(function($) { // $ is jQuery here