简体   繁体   English

使用Jquery将XML转换为HTML

[英]XML to HTML using Jquery

I am a new bee to Jquery looking for some help here. 我是Jquery的新手,正在这里寻求帮助。 The problem is pretty simple but i am explaining it in detail as there are multiple items I am referring to achieve my goal 这个问题很简单,但我要详细解释,因为我要实现目标涉及多个项目

1) I have used the following Jquery Plugin in my site http://tympanus.net/codrops/2011/12/14/item-blur-effect-with-css3-and-jquery/ 1)我在我的网站http://tympanus.net/codrops/2011/12/14/item-blur-effect-with-css3-and-jquery/中使用了以下Jquery插件

This plugin use modernizr.custom.34978.js and following Javascript to do the blur 该插件使用modernizr.custom.34978.js和以下Javascript进行模糊处理

$(function() {

                var $container  = $('#ib-container'),
                    $articles   = $container.children('article'),
                    timeout;

                $articles.on( 'mouseenter', function( event ) {

                    var $article    = $(this);
                    clearTimeout( timeout );
                    timeout = setTimeout( function() {

                        if( $article.hasClass('active') ) return false;

                        $articles.not( $article.removeClass('blur').addClass('active') )
                                 .removeClass('active')
                                 .addClass('blur');

                    }, 65 );

                });

                $container.on( 'mouseleave', function( event ) {

                    clearTimeout( timeout );
                    $articles.removeClass('active blur');

                });

            });

2) Following html tags are being used for this 2)以下html标记正用于此

<section class="ib-container" id="ib-container">
                <article>
                    <header>
                        <h3><a target="_blank" href="http://tympanus.net/codrops/2011/12/08/25-examples-of-perfect-color-combinations-in-web-design/">25 Examples of Perfect Color Combinations in Web Design</a></h3>
                        <span>December 8, 2011 by Gisele Muller</span>
                    </header>
                    <p>Today we will show you some examples of websites that are using beautiful and inspiring color combinations that match perfectly and create an eye candy...</p>
                </article>
</section>

I want to go one step ahead and update this html using xml file which is automatically generated using TSQL. 我想前进一步,并使用使用TSQL自动生成的xml文件更新此html。 The idea being, when a users submits details using web forms, the same is converted in xml using stored proc, which is turn is converted in html using following jquery 想法是,当用户使用Web表单提交详细信息时,使用存储的proc将其转换为xml,然后使用以下jquery将其转换为html

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "http://localhost:5732/js/ycube.xml",
    dataType: "xml",
    success: parseXml
  });
});

function parseXml(xml) {
    var list = $('#ib-container');

     $(xml).find("article").each(function (index, element) {

        var field = $(element)

        var link = field.find('a').text()
        var span = field.find('span').text()
         var para = field.find('p').text()

      .append('<article><header><h3><a target="_blank" href="http://tympanus.net/codrops/2011/12/08/25-examples-of-perfect-color-combinations-in-web-design/">' + link + '</a></h3><span>'+span+ '</span></header><p>'+ para + '</p></article>')

    ;
    })
}

And here is my xml file which is created using SQLserver query 这是我使用SQLserver查询创建的xml文件

?xml version="1.0" encoding="utf-8" ?>
<sepx>
        <article>
                    <header>
                        <h3><a target="_blank" href="http://tympanus.net/codrops/2011/12/08/25-examples-of-perfect-color-combinations-in-web-design/">25 Examples of Perfect Color Combinations in Web Design</a></h3>
                        <span>December 8, 2011 by Gisele Muller</span>
                    </header>
                    <p>Today we will show you some examples of websites that are using beautiful and inspiring color combinations that match perfectly and create an eye candy...</p>
                </article>
</sepx>

I am successful till this end, and the html tags are filled up as expected once jquery is called.All the major styles are also applied as expected, however the blur effect which should come using the above mentioned Jquery plugin (refer to bullet 1) is not happening. 直到最后我都成功了,并且一旦调用了jquery,html标记就会按预期填满。所有主要样式也都按预期应用,但是模糊效果应该使用上述Jquery插件来实现(请参考项目符号1)没有发生。

When I manually place the html code (refer to bullet 2), the pluing is working as expected. 当我手动放置html代码(请参阅项目符号2)时,粘贴工作按预期进行。

Can someone please help me here to understand why only blur effect is not working when I am pulling the details from XML, but the same works well when HTML is placed manually? 有人可以在这里帮助我了解为什么当我从XML中提取细节时,只有模糊效果不起作用,而当手动放置HTML时,效果很好吗? I have tried this in all major browser. 我已经在所有主流浏览器中尝试过了。

I again want to reiterate the face that I am a self taught in Jquery and HTML and all the above code snippts have been taken from multiple places and altered to suit my need. 我再次想重申一下,我是Jquery和HTML的自学者,以上所有代码段均已从多个地方获取并进行了更改以满足我的需要。

The articles you're retrieving via the AJAX call do not exist at the time you're binding the mouseenter handler. 绑定mouseenter处理程序时,通过AJAX调用检索的文章不存在。 As you cannot bind a handler to a non-existent DOM object, either use delegation (the better practice), or rebind the handler after the AJAX call (just as effective, but not as efficient). 由于您无法将处理程序绑定到不存在的DOM对象,因此请使用委托(更好的做法),或者在AJAX调用后重新绑定处理程序(同样有效,但效率不高)。

Delegation example: 委托示例:

$container.on('mouseenter', 'article', function (event) {
    var $article = $(this),
        $articles = $article.siblings();
    clearTimeout(timeout);
    timeout = setTimeout(function () {
        if ($article.hasClass('active')) return false;
        $articles.not($article.removeClass('blur').addClass('active'))
            .removeClass('active')
            .addClass('blur');
    }, 65);
});

Rebinding example: 重新绑定示例:

$(document).ready(function () {
    var bindMouseEnterHandler = function bindMouseEnterHandler() {
            var $container = $('#ib-container'),
                $articles = $container.children('article'),
                timeout;
            $articles.unbind('mouseenter').on('mouseenter', function (event) {
                var $article = $(this);
                clearTimeout(timeout);
                timeout = setTimeout(function () {
                    if ($article.hasClass('active')) return false;
                    $articles.not($article.removeClass('blur').addClass('active'))
                        .removeClass('active')
                        .addClass('blur');
                }, 65);
            });
            $container.on('mouseleave', function (event) {
                clearTimeout(timeout);
                $articles.removeClass('active blur');
            });
        },
        parseXml = function parseXml(xml) {
            var list = $('#ib-container');
            $(xml).find("article").each(function (index, element) {
                var field = $(element)
                var link = field.find('a').text()
                var span = field.find('span').text()
                var para = field.find('p').text()
                list.append('<article><header><h3><a target="_blank" href="http://tympanus.net/codrops/2011/12/08/25-examples-of-perfect-color-combinations-in-web-design/">' + link + '</a></h3><span>' + span + '</span></header><p>' + para + '</p></article>');
                bindMouseEnterHandler();
            })
        };
    $.ajax({
        type: "GET",
        url: "http://localhost:5732/js/ycube.xml",
        dataType: "xml",
        success: parseXml
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM