开发者

Word having single quotes search from xml file using jquery issue

Hi I need to parse XML file using jquery. I created read and display functionality. But when a word having single quote not working.

My XML is like this

<container>
<data name="Google" definition="A search engine"/>
<data name=" Mozilla's " definition="A web browser"/>
</ container>

using my jquery code I can read definition of Google. But I can't read Mozilla's definition due to that single quotes. This is my jquery code.


    var displayDefinition = function(obj){
        $.get("definitions.xml", function(data){
         xml_data1.find("data[name^='"+obj.innerHTML+"']").each(function(k, v){


     right='

'+ $(this).attr("Defination") + '

'; } } $(开发者_开发百科".result").append(right); }

Any body knows the solution for this please help me. Thanks


jQuery deals with single quotes very well. the structure of your function looks really wild though. I changed it a big assuming you want to create a function that can display the definition based on passing it a name: http://jsfiddle.net/rkw79/VQxZ2/

function display(id) {
    $('container').find('data[name="' +id.trim()+ '"]').each(function() {
        var right = $(this).attr("definition");
        $(".result").html(right);
    });
}

Note, you have to make sure your 'name' attribute does not begin or end with spaces; and just trim the string that the user passes in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜