0


我最近开始学习 Javascript 以在工作中帮助我,我不禁认为我咬掉的东西比我能咀嚼的要多得多。

想法:
使用表单,获取单个输入信息,使用预定义值为信息添加前缀和后缀,并将其输出到文本区域,以便我可以复制/使用它。

出于某种原因,到目前为止,我发现学习 javascript 特别困难,而且我编写的代码确实可以正常工作,这正是我想要的,但我不禁认为我编写的代码非常草率。

表格

<form name="invalidateForm"  action="">
Image Name:<br /><input id="imageName" type="text" name="imageName" value=""><br />
<input class="btn" type="button" onclick="invalidateUrls()" value="Generate URLs">
<input class="btn" type="reset"><br />           
<textarea id="icdnResults"></textarea>
</form>

JavaScript

<script type="text/javascript">
function invalidateUrls()
{
var txt = "";
document.getElementById("icdnResults").value = "";

if (document.getElementById("imageName").value != ""){
txt = "";

//Category Thumbnails
txt += "prefix text";
txt += document.getElementById("imageName").value;
txt += "suffix text\n";

//Product Page Main Image
txt += "prefix text 2";
txt += document.getElementById("imageName").value;
txt += "suffix text 2\n";

//Flyout image on product page
txt += "prefix text 3";
txt += document.getElementById("imageName").value;
txt += "suffix text 3\n";

//Product page thumbnails
txt += "prefix text 4";
txt += document.getElementById("imageName").value;
txt += "suffix text 4\n";

document.getElementById("icdnResults").value += txt;
}
}
</script>

我有 99% 的把握,我所做的比草率更进一步,而且我也猜测它一点也不“面向未来”。

我应该更多地使用和定义变量,还是有完全不同的方法?

如果有人能判断我是否走在正确的轨道上,或者我是否应该完全废弃它,将不胜感激。

谢谢 :) 亚历克斯

编辑
我向读过这篇文章的任何人道歉,看来我没有正确解释并忘记提及一件关键的事情;前缀和后缀都有 4 个不同的字符串。因此,用户将在输入字段中输入一个值,它会以 4 个不同的值作为前缀,并以 4 个不同的值作为后缀。

http://www.somedomain.com/folder/?fmy=<<USER_INPUT>>&type=etc
http://www.somedomain.com/folder/?someCmd=<<USER_INPUT>>&layer=etc
http://www.somedomain.com/folder/?itemId=<<USER_INPUT>>&value=etc
http://www.somedomain.com/folder/?pageR=<<USER_INPUT>>&caption=etc

那么“?”在哪里?有 4 个不同的值,其中“&”也有 4 个完全不同的值。

编辑 2
好的,根据约翰的回复......我已经稍微调整了一下。它看起来怎么样?作为我第一次尝试更改代码的垃圾?

var processForm = function(){
var value = $('#imageName').val(),
    outputElement = $('#icdnResults'),
    html = '',
    s_imageTag = function(size){
        return size + value;
    },

    e_imageTag = function(size2){
        return size2 + "\n";
    },

   size = ['s_catThumb', 's_productMain'], 
   size2 = ['e_catThumb', 'e_productMain'];

html += s_imageTag('s_catThumb ');
html += e_imageTag(' e_catThumb');
html += s_imageTag('s_productMain '); 
html += e_imageTag(' e_productMain'); 

outputElement.val(html);
};

$(function(){
$('#gen').on('click', processForm);
});
4

2 回答 2

1

也许是这样的事情?

http://jsfiddle.net/nvLx9/2/

HTML

<input id="user_input" type="text" placeholder="User Input" value="test">
<button id="process" type="button">Process</button>
<br>
<textarea id="output" row="4" cols="60" style="height:60px"></textarea>

<div id="images">
</div>

JS

var getLinks = function(value){
        var links = [
                'http://placehold.it/100x100/ff0000&text=<<USER_INPUT>>',
                'http://placehold.it/100x100/0000ff&text=<<USER_INPUT>>',
                'http://placehold.it/100x100/00ff00&text=<<USER_INPUT>>',
                'http://placehold.it/100x100/ff00ff&text=<<USER_INPUT>>',
            ];

            $.each(links, function(idx, link){
                links[idx] = link.replace('<<USER_INPUT>>', value);
            });

            return links;
    },
    processForm = function(){
        var value = $('#user_input').val(),
            links = getLinks(value),
            getText = function(){
                var h = '';
                $.each(links, function(idx, link){
                    h += link + "\n";
                });
                return h;
            },
            addImages = function(){
                var h = '';

                $.each(links, function(idx, link){
                    h += '<img src="'+link+'">';
                });

                $('#images').html(h);
            };

        $('#output').val(getText());
        addImages();
    };

$('#process').on('click', processForm);
于 2013-11-07T09:39:25.753 回答
1

As above but without the use of an additional library. JQuery is great and if you are going to be doing a lot of JavaScript you may find it easier to work with. But if you would rather not add a dependency on an additional library the same can be done using vanilla JS:

http://jsfiddle.net/4GtFe/1/

HTML

<form name="invalidateForm" action="">Image Name:<br />
    <input id="imageName" type="text" name="imageName" value=""><br />
    <input class="btn" type="button" value="Generate URLs" id="gen">
    <input class="btn" type="reset"><br />
    <textarea id="icdnResults"></textarea>
</form>

JS

var processForm = function () {
    var value = document.getElementById('imageName').value,
        html = '',
        imageTag = function (size) {
            return 'prefix ' + size + ' <<' + value + '>> suffix ' + size + "\n";
        },
        sizes = ['text', 'text 2', 'text 3'];


    for (var i = 0; i < sizes.length; i++) {
        html += imageTag(sizes[i]);
    }

    document.getElementById('icdnResults').value = html;
};

document.getElementById('gen').addEventListener('click', function() {
    processForm();
}, false);

As shown in both my answer and the answer above; it is good practice to use unobtrusive javascript where possible. This will keep your code clean and improves readability and maintainability.

Additionally if you wish to support Internet Explorer 8 and older you will need to replace addEventListener with a function to handle it's support, see this post addEventListener in Internet Explorer.

于 2013-11-07T10:37:05.790 回答