jQuery在HTML标签处拆分,即在h2标签的每个实例处拆分? [英] jQuery split at HTML tags i.e split at every instance of the h2 tags?

查看:129
本文介绍了jQuery在HTML标签处拆分,即在h2标签的每个实例处拆分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到所有<h2>标记,将它们拆分,然后将它们与周围的<a href=''></a>结合在一起.我太近了,但是被困住了.

I am trying to find all <h2> tags the split them and join them with <a href=''></a> around them. I am so close but stuck.

<script type="application/javascript">
$(document).ready(function() {


// finds all h2's within wrapper div grabs the text splits at ? and applies a links around each one
var al = $('#wrapper').find('h2').text().split("?").join("?</a><br /> <a  href='#'>");

// Add all the split h2's from the variable above al to a div called .text
$('.text').html('<ul>' + al + '</ul>');


});      
</script>

这是我来自alert(al)的输出:

This is my output from alert(al):

Appropriate Media – Why radio?</a><br /> <a  href='#'>Can someone come and speak at my church?</a><br /> <a  href='#'>Do you distribute radios?</a><br /> <a  href='#'>Do you partner with other organisations?</a><br /> <a  href='#'>How is Feba funded?</a><br /> <a  href='#'>What are your programmes about?</a><br /> <a  href='#'>What denomination does Feba belong to?</a><br /> <a  href='#'>What happened to the Chrysolite?</a><br /> <a  href='#'>What is airtime?</a><br /> <a  href='#'>What is Feba's Statement of Faith?</a><br /> <a  href='#'>Where are the programmes made?</a><br /> <a  href='#'>Where can I find out about the languages & countries you broadcast in?</a><br /> <a  href='#'>Where does the name Feba come from?</a><br /> <a  href='#'>Who do you broadcast to?</a><br /> <a  href='#'>Why do you broadcast on short wave?</a><br /> <a  href='#'> 

好吧,现在我可以在?处拆分它们,因为每个问题都以?结尾,但是我的问题是这遗漏了第一个问题.

Ok so at the moment I am able to split them at the ? because each question end with a ?, but my issue is this misses out the first question.

所以我的解决方案是在<h2>标签处拆分它们,还是我尝试了那么多次的更好的选择?

So my solution would be to split them at the <h2> tags is this possible or is there a better option i have tried so many?

推荐答案

出色的工作非常感谢.我只是想了解代码,以便将来使用.我对jquery还是很陌生,想学习;)

Brilliant this worked thanks a lot. I am just trying to understand the code so i can utilize it in the future. I am pretty new to jquery and want to learn ;)

这是我正在处理的网页和完整代码,因此您可以看到结果.您可能会发现一种更好的方法.

Here is the webpage i am working on and full code so you can see the result. You might see an better way off doing it.

http://www.giveradio.org.uk/faqs

<script type="application/javascript">
    $(document).ready(function() {

    var al = $('.text');

    $('h2').each(function(){
        $('<a>', {
            text: $(this).text(),
            href: '#'
        }).wrap('<li>').parent().appendTo(al);
    });

    $('.text a').click(function(e) {

    e.preventDefault(); 

    // Removes h2 class jump which is added below
    $('h2').removeClass('jump');

    // Grabs the text from this a link that has been clicked
    var alink = $(this).text(); 

    // filters through all h2's and finds a match off text above and add class jump
    $('h2:contains("' + alink +'")').addClass("jump");

    // scrollto the h2 with the class jump
    $('html,body').animate({scrollTop: $("h2.jump").offset().top},'fast');

    return false;

    }); 


    });                  
    </script>

这篇关于jQuery在HTML标签处拆分,即在h2标签的每个实例处拆分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆