Javascript 按类名查找元素
我有以下问题: 在我的身体里,我有这个div结构:Javascript 按类名查找元素,javascript,class,find,element,Javascript,Class,Find,Element,我有以下问题: 在我的身体里,我有这个div结构: <div id="page-wrapper"> <div id="page"> <div id="page-content" class="page-inner"> <div id="page-content-inner"> <div class="box tagcloud">
<div id="page-wrapper">
<div id="page">
<div id="page-content" class="page-inner">
<div id="page-content-inner">
<div class="box tagcloud">
<div class="modulcontent">
<div class="tagspopular tagcloud tagscloud tagcloud">
<span class="tag" >
<a class="tag-name" > test1</a>
</span>
<span class="tag">
<a class="tag-name" >test2</a>
</span>
<span class="tag">
<a class="tag-name" >test3</a>
</span>
<span class="tag">
<a class="tag-name" > test4</a>
</span>
<span class="tag">
<a class="tag-name" >test5</a>
</span>
<span class="tag">
<a class="tag-name" >test6</a>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
然后我尝试使用jquery:
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery-noconflict.js" type="text/javascript"></script>
<script src="jquery-migrate.js" type="text/javascript"></script>
<script type="text/javascript">
var pre = $('.tag');
pre.each(function(i, el){
console.log( el );
// OR
console.log( this ); // will give you the element
// suppose to get text
console.log( $(el).text() ); // or $(this).text()
});
</script>
var pre=$('.tag');
每个(功能(i、el)前{
控制台日志(el);
//或
console.log(this);//将为您提供元素
//假设你收到了短信
console.log($(el.text());//或$(this.text())
});
当我尝试此操作时,我得到一个错误:TypeError:$不是一个函数var pre=$('.tag')代码>
我真的不知道该怎么办。我从这里得到了两个代码,但仍然不起作用(您应该使用.ready()
在DOM完全加载时执行代码
$( document ).ready(function() {
var pre = $('.tag');
pre.each(function(i, el){
console.log( el );
// OR
console.log( this ); // will give you the element
// suppose to get text
console.log( $(el).text() ); // or $(this).text()
});
});
您应该使用.ready()
在DOM完全加载时执行代码
$( document ).ready(function() {
var pre = $('.tag');
pre.each(function(i, el){
console.log( el );
// OR
console.log( this ); // will give you the element
// suppose to get text
console.log( $(el).text() ); // or $(this).text()
});
});
您应该使用.ready()
在DOM完全加载时执行代码
$( document ).ready(function() {
var pre = $('.tag');
pre.each(function(i, el){
console.log( el );
// OR
console.log( this ); // will give you the element
// suppose to get text
console.log( $(el).text() ); // or $(this).text()
});
});
您应该使用.ready()
在DOM完全加载时执行代码
$( document ).ready(function() {
var pre = $('.tag');
pre.each(function(i, el){
console.log( el );
// OR
console.log( this ); // will give you the element
// suppose to get text
console.log( $(el).text() ); // or $(this).text()
});
});
您在无冲突模式下运行它,因此即使库正在加载,您也应该使用jQuery('.tag')
。正常情况下您不需要noconflict。如果您不需要noconflict(或者您不知道这意味着什么),请删除jQuery noconflict.js
。什么不适用于非jQuery示例?控制台保持为空,不检测标记-elements@epascarello:我将$('.tag')更改为jQuery('.tag')。现在我没有收到任何错误,但控制台仍然是空的:(您在无冲突模式下运行它,所以应该使用jQuery('.tag'))
即使库正在加载,也是如此。在正常情况下,您不需要noconflict。如果您不需要noconflict(或者您不知道这意味着什么),请删除jquery noconflict.js
。对于非jquery示例,什么不起作用?控制台保持为空,它不会检测到标记-elements@epascarello例如我换了房间$('.tag')进入jQuery('.tag')。现在我没有收到任何错误,但控制台仍然是空的:(您正在以无冲突模式运行它,所以即使库正在加载,您也应该使用jQuery('.tag')
。正常情况下您不需要noconflict。如果不需要noconflict,请删除jQuery noconflict.js
)(或者你不知道这意味着什么)。你的非jQuery示例有什么不起作用?控制台保持为空,它没有检测到标记-elements@epascarello:我将$('.tag')更改为jQuery('.tag')。现在我没有收到任何错误,但控制台仍然是空的:(您在无冲突模式下运行它,所以应该使用jQuery('.tag'))
即使库正在加载,也是如此。在正常情况下,您不需要noconflict。如果您不需要noconflict(或者您不知道这意味着什么),请删除jquery noconflict.js
。对于非jquery示例,什么不起作用?控制台保持为空,它不会检测到标记-elements@epascarello例如我换了房间$('.tag')转换为jQuery('.tag')。现在我没有收到任何错误,但控制台仍然是空的:(非常感谢。$commands也不起作用。我现在将所有$s都改为jQuery,现在我得到了我想要的。非常感谢。同时也感谢所有其他人的帮助!不客气。如果答案有用,请将其标记为正确。非常感谢。$commands也不起作用。我现在将所有$s都改为jQuery,现在我得到了我想要的。非常感谢。也感谢所有其他人的帮助!不客气。如果答案有用,请将其标记为正确。非常感谢。$命令也不起作用。我现在将所有$更改为jQuery,现在我得到了我想要的。非常感谢。也感谢所有其他人的帮助!不客气。如果答案有用,请将我标记为I不太正确。非常感谢。$命令也不起作用。我现在将所有$都改为jQuery,现在我得到了我想要的。非常感谢。同时也感谢所有其他人的帮助!不客气。如果答案有用,请将其标记为正确。