我想在页面加载后设置元素的宽度。为此我使用这个脚本:
<script type='text/javascript' src='https://radlvoo.de/wp-includes/js/jquery/jquery.js'>
jQuery(document).ready(function() {
var y = document.getElementsByClassName("col-sm-6");
for (var i = 0; i < y.length; i += 1){
y[i].style.width = "100%";
}
});
</script>
但它不起作用。如果我用实际函数替换jQuery(document).ready(function() {
,请说changeWidth()
它正在运行..
亲自尝试here。你可以看到,如果你在控制台中调用changeWidth()
,这个会扩展:
enter image description here
此外,如果我将其遗漏type='text/javascript' src='https://radlvoo.de/wp-includes/js/jquery/jquery.js'
,则说:
jQuery is not defined
这是什么原因以及如何解决这个问题?
亲切的问候
答案 0 :(得分:2)
您的代码应为
<script type='text/javascript' src='https://radlvoo.de/wp-includes/js/jquery/jquery.js'></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var y = document.getElementsByClassName("col-sm-6");
for (var i = 0; i < y.length; i += 1){
y[i].style.width = "100%";
}
});
</script>
答案 1 :(得分:0)
您应该使用以下代码:
<script type='text/javascript' src='https://radlvoo.de/wp-includes/js/jquery/jquery.js'></script>
<script type='text/javascript'>
jQuery(document).ready(function() {
var y = jQuery(".col-sm-6");
y.each(function() {
$(this).css('width', '100%');
});
});
</script>