Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用一个元素';父标题的文本';s ID值_Javascript_Jquery - Fatal编程技术网

Javascript 使用一个元素';父标题的文本';s ID值

Javascript 使用一个元素';父标题的文本';s ID值,javascript,jquery,Javascript,Jquery,在我的页面上,我有以下HTML: <div class="Module-item"><!-- list --> <div class="AnchorContainer" id=""> <h1> <span class="InsertAsAnchor">Services</span> </h1> <div class="clea

在我的页面上,我有以下HTML:

<div class="Module-item"><!-- list -->

    <div class="AnchorContainer" id="">
        <h1>
            <span class="InsertAsAnchor">Services</span>
        </h1>
        <div class="clear"></div>
    </div>

</div>

我希望有人能告诉我如何以正确的方式做到这一点,

既然你是新人,我就让你放松一下。试试这个:

$('.Module-item h1 > span, .Module-item h2 > span, .Module-item h3 > span')
  .each(function() {
    var myText = $(this).text().replace(' ','');
    $(this).parent().attr('id', myText);
});

以后,请先尝试一下。

既然你是新来的,我就让你放松一下。试试这个:

$('.Module-item h1 > span, .Module-item h2 > span, .Module-item h3 > span')
  .each(function() {
    var myText = $(this).text().replace(' ','');
    $(this).parent().attr('id', myText);
});

以后,请先尝试一下。

既然你是新来的,我就让你放松一下。试试这个:

$('.Module-item h1 > span, .Module-item h2 > span, .Module-item h3 > span')
  .each(function() {
    var myText = $(this).text().replace(' ','');
    $(this).parent().attr('id', myText);
});

以后,请先尝试一下。

既然你是新来的,我就让你放松一下。试试这个:

$('.Module-item h1 > span, .Module-item h2 > span, .Module-item h3 > span')
  .each(function() {
    var myText = $(this).text().replace(' ','');
    $(this).parent().attr('id', myText);
});
以后,请先尝试一下。

我就是这样做的

我就是这样做的

我就是这样做的

我就是这样做的

这应该做到:

$('.Module-item > .AnchorContainer').find('h1,h2,h3,h4,h5,h6').attr('id', function() {
     var sp = $('>span:first', this);
     if( sp.length === 1 || sp.text().trim().length > 0 ) {
       return sp.text().trim().replace(/ /g,'');
     }
});
$('.Module item>.AnchorContainer')。查找('h1、h2、h3、h4、h5、h6')。属性('id',函数(){
var sp=$('>span:first',this);
如果(sp.length==1 | | sp.text().trim().length>0){
返回sp.text().trim().replace(//g',);
}
});

服务
我们有很多服务
他们的服务
这应该可以做到:

$('.Module-item > .AnchorContainer').find('h1,h2,h3,h4,h5,h6').attr('id', function() {
     var sp = $('>span:first', this);
     if( sp.length === 1 || sp.text().trim().length > 0 ) {
       return sp.text().trim().replace(/ /g,'');
     }
});
$('.Module item>.AnchorContainer')。查找('h1、h2、h3、h4、h5、h6')。属性('id',函数(){
var sp=$('>span:first',this);
如果(sp.length==1 | | sp.text().trim().length>0){
返回sp.text().trim().replace(//g',);
}
});

服务
我们有很多服务
他们的服务
这应该可以做到:

$('.Module-item > .AnchorContainer').find('h1,h2,h3,h4,h5,h6').attr('id', function() {
     var sp = $('>span:first', this);
     if( sp.length === 1 || sp.text().trim().length > 0 ) {
       return sp.text().trim().replace(/ /g,'');
     }
});
$('.Module item>.AnchorContainer')。查找('h1、h2、h3、h4、h5、h6')。属性('id',函数(){
var sp=$('>span:first',this);
如果(sp.length==1 | | sp.text().trim().length>0){
返回sp.text().trim().replace(//g',);
}
});

服务
我们有很多服务
他们的服务
这应该可以做到:

$('.Module-item > .AnchorContainer').find('h1,h2,h3,h4,h5,h6').attr('id', function() {
     var sp = $('>span:first', this);
     if( sp.length === 1 || sp.text().trim().length > 0 ) {
       return sp.text().trim().replace(/ /g,'');
     }
});
$('.Module item>.AnchorContainer')。查找('h1、h2、h3、h4、h5、h6')。属性('id',函数(){
var sp=$('>span:first',this);
如果(sp.length==1 | | sp.text().trim().length>0){
返回sp.text().trim().replace(//g',);
}
});

服务
我们有很多服务
他们的服务


您想做的一切都可以轻松搜索。试试看,让我们看看什么不管用!JSFIDLE就要来了。你想做的一切都很容易搜索。试试看,让我们看看什么不管用!JSFIDLE就要来了。你想做的一切都很容易搜索。试试看,让我们看看什么不管用!JSFIDLE就要来了。你想做的一切都很容易搜索。试试看,让我们看看什么不管用!JSFIDLE即将发布。非常感谢您提供的链接。这肯定会有帮助的!非常感谢您提供的链接。这肯定会有帮助的!非常感谢您提供的链接。这肯定会有帮助的!非常感谢您提供的链接。这肯定会有帮助的!这是als的作品,谢谢你的努力!没问题。我只是更新了它,使它更简短,更容易理解。这个als的工作,谢谢你的努力!没问题。我只是更新了它,使它更简短,更容易理解。这个als的工作,谢谢你的努力!没问题。我只是更新了它,使它更简短,更容易理解。这个als的工作,谢谢你的努力!没问题。我只是更新了它,使它更简短,更容易理解。