English 中文(简体)
动态电话功能
原标题:dynamically call function javascript

我想做的是,把所有的 Java本同页分开,这样它就使所有人都能使用残疾的 Java本,但也能够使用封闭式汇编器的先进汇编(http://closure-compiler.appspot.com/home)。

在这方面,我是如何开始的:

......

<a href="javascript:doMultiple( download ,  release );">Create archive</a>

to...

<a class="javascript doMultiple download release" href="ca.php">CreateArchive</a>

Then I loop all objects to find what have the class javascript first, etc: (links is an array of the a tags)

for (i = 0; i < links.length; i = i + 1) {
    if (links[i].className.substr(0, 10) === "javascript") {
        jsArray = links[i].className.split(" ");
        links[i].style.display = "inline";
        if (links[i].addEventListener) {
            links[i].addEventListener("click", window[jsArray[1]], false);
        } else {
            links[i].onclick = window[jsArray[1]];
        }
        links[i].href = "#";
    }
}

采用这种方法, j字压缩机将取消Multiple功能,因为其未使用,因为它知道我有一等同。

我的问题是,我如何解决这一问题,如果我能够说的话,我能否在某种程度上摆脱窗户[jsArray?],以另一种方式解决这一问题? 因为我认为这不是这样做的最佳方式。

也可以是:

<a class="javascript popUp register.php 350 300" href="register.php">Register</a>

So I want to use this method for adding all JavaScript dynamically.

Best regards,
Johan Svensson

最佳回答

你不需要使事情复杂化。 最好在您的OM要素下举办活动。

我非常建议你采用 Java文框架,如 j文,甚至你也可以使用简便的 Java本做到这一点。

<a href="#" id="createArchive" class="remove otherClass">Create archive</a>

页: 1

<a href="#" id="createArchive" class="release otherClass">Create archive</a>

Java:

var element = document.getElementById("createArchive");

var handler = function() {
        // this line will execute only on IE
        // you need an if-else case to test
        // against the browser other than IE
        // and use this.class.split
        var classes = this.className.split(   );

        doMultiple( download , classes[0]);
        // or anything you want to on this click event
};

if (element && element.attachEvent) {
    // This is for IE
    element.attachEvent("onclick", handler);
}
else if (element && element.addEventListener) {
    // This is for rest of the world
    element.addEventListener("click", handler, false);
}

And if you use jQuery, life is simple

$("#createArchive").bind("click", function() {
    doMultiple( download ,  release );
});

这符合要求,也铭记没有。 Javagust/代码,载于您的超文本。

Question 2

对于谷歌封闭器,你必须利用你不想汇编/确定的职能。

页: 1 JS file which will contained the function name to be not.

页: 1

function doMultiple() { }
function popUp() { }

And run this from your command line to compile using closure compiler

java -jar .compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js input_script_file.js --js_output_file=compiled.min.js --externs exclude.js

希望这一帮助。 请允许我自由提问。

问题回答

首先,usevar 地方变量关键词。 变式<代码>jsArray是全球性的,因为你没有用<代码>var<>/code>的关键词来预先确定,这些词基本上是错误的(在这种情况下不应是全球性的)。

第二,可以通过<条码>数据- 标的属性获取额外数据。 因此:

<a class="javascript popUp register.php 350 300" href="register.php">Register</a>

can be changed to the more meaningful:

<a data-window-type="popUp" data-popup-height="350" data-popup-width="300" href="register.php">Register</a>

因此,你以前储存在<代码><>}/代码>属性(除非是多余的)内的所有数据。 获取否决权是简单明了的,因为可以接触到任何其他方面。

在谈到你目前的法典时,我认为它过于重复,我不相信你想要达到什么目标。 遵循上述规则,你的法典可能非常简单。

I m glad that someone is trying to get rid of "javascript:" calls from links. :-D

它非常简单。 把它分为“等级”步骤,这是你做的:

  1. Loop through the links which have the class name you specified;
  2. Bind them to an event listener that will trigger whatever the action is.

As an initial note, you should think of a better approach to handle this. Simply "converting" a JavaScript call to that isn t something good because it will 1) require ugly hacks to accomplish what you want and 2) still go against the content semantics: classes names are content too; they re not intended to serve as behavior or styling.

After you think of a better approach -- I m sure you ll find one easily --, you ll basically do what s you re already doing; it just needs some JavaScript love:

第一,确定行动。

function the_action(event)
{
    // Prevents the browser of following the link s href.
    // "javascript:void(0)" or "#" are for dumbs. ;-)
    event.preventDefault();

    // You can reuse the original href!
    // (like any other attribute or element property)
    // "this" is the DOM element.
    var link_url = this.getAttribute( href );

    alert( lol! );
}

现在,通过这些联系和增加听众的更好途径:

var selector =  a.foobar 
  , links = document.querySelectorAll(selector);

i = links.length; while(i--)
{
    links[i].addEventListener( click , the_action, false);
}

Note that I am defining the function before the loop because if I put it inside the addEventListener call, it ll create a new Function object for each element in the list.

Note also that, in the above snippets, I used code available only in modern browsers. If you have to support IE, I recommend you to get a simple JavaScript library that will "standardize" things. About this, you can choose jQuery -- I dislike it, but that s for another long conversation --, Mootools, Mandoo and others. It s about philosophy/taste. :-)

建立一个图书馆也将使你的法典更加清洁。 公正不!!

亲爱!





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.