Javascript jqueryfind不';行不通

Javascript jqueryfind不';行不通,javascript,jquery,Javascript,Jquery,当span的父级(li)包含一个类为“checked”的span时,获取span的内容(本例中为Arrière)有一个问题 这是我的html <fieldset class="attribute_fieldset"> <label class="attribute_label">Base du siège &nbsp;</label> <div class="attribute_list">

当span的父级(li)包含一个类为“checked”的span时,获取span的内容(本例中为Arrière)有一个问题

这是我的html

<fieldset class="attribute_fieldset">
    <label class="attribute_label">Base du siège &nbsp;</label>
        <div class="attribute_list">
            <ul>
                <li>
                    <div class="radio">
                        <span class="checked">
                            <input type="radio" class="attribute_radio" name="group_4" value="26" checked="checked">           
                        </span>
                    </div>
                   <span>Arrière</span>
                </li>
            </ul>
        </div> <!-- end attribute_list -->
</fieldset>

西耶格基地
  • 阿里埃
我尝试了以下jquery代码:

$('.attribute_fieldset').each(function( index ) {
    var attribute_label = $(this).children('.attribute_label').html();
    var attributeval = $(this).find('.checked').parent('.radio').next('span').html();
    console.log(attributeval);
    var that = this;    
  $('#product_attributes').append('<li>'+attribute_label.replace('&nbsp;','')+':');

});
$('.attribute_fieldset')。每个(函数(索引){
var attribute_label=$(this).children('.attribute_label').html();
var attributeval=$(this).find('.checked').parent('.radio').next('span').html();
console.log(attributeval);
var=这个;
$(“#产品_属性”)。追加(“
  • ”+属性_标签。替换(“,”)+:”); });
  • 我未定义attributeval

    这应该可以:

    $('.attribute_fieldset').each(function() {
       $('#product_attributes').append('<li>' + $(this).find('attribute_label').html().replace(' &nbsp;', '') + ':' + $(this).find('li span.checked').parent().next('span').html() + '</li>');
    });
    
    $('.attribute_fieldset')。每个(函数(){
    $(“#产品_属性”).append(“
  • ”+$(this).find('attribute_label').html().replace(“,”)+”:“+$(this).find('li span.checked').parent().next('span').html()+”
  • ); });
    工作正常可能您没有包括脚本,因为^worksWorking正常工作。对于给定的html和css。我猜您的脚本之前没有jquery,并且有一个$is未定义的错误,对吧!不它不会使用li span.checked选择器获得目标span.will,但在文档加载事件中,我得到了未定义的变量,我认为dom还没有准备好yet@tarekfellah然后只需将代码片段放入:$(document.ready(function(){………..});它已经在一个文档中准备好了,页面太慢,dom还没有加载,我已经添加了一个超时来让它工作,考虑到$(document.ready)(function(){………..});专门用于在加载所有DOM元素后等待JS代码开始执行。您不需要为此设置超时。如果您的页面非常繁重,加载时间较长,则应考虑将所有JS代码打包在Read()声明中;好的,那么你应该考虑使用$(window).Load(函数()){…}而不是Read()。有关更多信息,请参阅文档()
    Instead you can try below code:
    $(".attribute_fieldset span").each(function(index, spanElement) {
        if($(spanElement).parent().find("span.checked").length
             && !$(spanElement).hasClass("checked")) {
            console.log(spanElement); // this is the span element you want
            console.log(spanElement.text()); // this is the value "Arrière"
          }
    });