Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 JS属性值onclick replace_Javascript_Jquery_Onclick_Codemirror - Fatal编程技术网

Javascript JS属性值onclick replace

Javascript JS属性值onclick replace,javascript,jquery,onclick,codemirror,Javascript,Jquery,Onclick,Codemirror,我使用codemirror和jquery在浏览器中“模拟”xml编辑器。一些xml标记包含一个“on”属性,该属性有两个可能的值(true或false)。是否可以在onclick事件中切换这些值?codemirror/jquery插件可用吗 编辑: 自编码解决方案 到目前为止还不是完美的(糟糕的设计等等),但它的工作原理与预期相符: 只需在onclick事件中触发该函数 function attributeToggle(){ var transitions = { "o

我使用codemirror和jquery在浏览器中“模拟”xml编辑器。一些xml标记包含一个“on”属性,该属性有两个可能的值(true或false)。是否可以在onclick事件中切换这些值?codemirror/jquery插件可用吗

编辑:

自编码解决方案

到目前为止还不是完美的(糟糕的设计等等),但它的工作原理与预期相符:

只需在onclick事件中触发该函数

function attributeToggle(){

    var transitions = {
        "on": {
            "false":"true",
            "true":"false"
        }
    }


    var pos = editor.getCursor(); // object {line, ch}
    var token = editor.getTokenAt(pos);
    var line = editor.getLine(pos.line);

    try{

        var prev_pos = token.start - 1;
        var prev_token = editor.getTokenAt({'line':pos.line, 'ch':prev_pos});

        if(prev_token.className == "attribute")
            var attr = prev_token.string;
        else
            return false;

        if(typeof transitions[attr] === "undefined") //nothing to replace
            return false;

        var current_val = token.string.toLowerCase().replace(/(['"])/g, "");


        if(typeof transitions[attr][current_val] === "undefined")
            return false;

        var line_new =  line.substring(0, token.start) + \
 "\"" + transitions[attr][current_val] + "\"" +  line.substring(token.end);

        editor.setLine(pos.line, line_new);


    } catch (e){
        console.log(e);
    }
}

只是想澄清一下:您正在CodeMirror中编辑XML,并且拥有类似于
的XML,当用户在文本编辑器中单击该节点时,您希望将其更改为
?是的!哪条路最好?
$(".CodeMirror").attr("onclick","javascript:attrtoggle()");
function attributeToggle(){

    var transitions = {
        "on": {
            "false":"true",
            "true":"false"
        }
    }


    var pos = editor.getCursor(); // object {line, ch}
    var token = editor.getTokenAt(pos);
    var line = editor.getLine(pos.line);

    try{

        var prev_pos = token.start - 1;
        var prev_token = editor.getTokenAt({'line':pos.line, 'ch':prev_pos});

        if(prev_token.className == "attribute")
            var attr = prev_token.string;
        else
            return false;

        if(typeof transitions[attr] === "undefined") //nothing to replace
            return false;

        var current_val = token.string.toLowerCase().replace(/(['"])/g, "");


        if(typeof transitions[attr][current_val] === "undefined")
            return false;

        var line_new =  line.substring(0, token.start) + \
 "\"" + transitions[attr][current_val] + "\"" +  line.substring(token.end);

        editor.setLine(pos.line, line_new);


    } catch (e){
        console.log(e);
    }
}