简体   繁体   English

在上运行javascript函数

[英]Running a javascript function on a <a href>

I was just wondering how to trigger a JavaScript function on a <a> like this: 我只是想知道如何在<a>上触发这样的JavaScript函数:

$('a').click( function() { your_code_here; return false; } );

and then using this to run a JavaScript function that runs something like this: 然后使用它来运行运行如下所示的JavaScript函数:

$.post("queries.php")

that runs a PHP function that runs a SQL query like this: 运行PHP函数,该函数运行如下SQL查询:

public function GetResults()
{ 
    list($subjectID, $sectionID, $principleID) = explode('#', $_POST["comboboxselections"]);
    $box = ""; // placeholder for the html results to be returned to 
    $sql = "SELECT media_id,title,blurb 
            FROM media 
            WHERE subject_id = $subjectID AND section_id = $sectionID AND principle_id= $principleID AND verified = 1"; 
    try
    {
        return $this->getResultsAndGenerateHTML($sql, $this->conn);
    }catch(exception $e)
    {
        echo 'Caught exception: ',  $e->getMessage(), "\n";

    }
}
  1. Would this be the best option to run a SQL query from a HTML <a> or is there a better way of doing this? 这是从HTML <a>运行SQL查询的最佳选择,还是有更好的方法呢?

  2. I already know about MySQLi and PDO this project is not going live so I do not need to worry about SQL injection. 我已经了解MySQLi和PDO,该项目无法上线,因此我不必担心SQL注入。

  3. It has to be done on a HTML <a> . 必须在HTML <a> I know I could use a button which would be easier, but with my specification, it uses a <a> . 我知道我可以使用一个更简单的按钮,但是根据我的说明,它使用了<a>

e.preventDefault(); 

prevents the default action of clicking he link and allows us to do our stuff. 阻止点击链接的默认操作,并允许我们执行操作。

$("a").click(function(e){

e.preventDefault();

//Do ur stuff

});

Visit this link: 访问此链接:

http://api.jquery.com/event.preventdefault/ http://api.jquery.com/event.preventdefault/

They have beautifully explained its functionality with examples 他们通过示例精美地解释了其功能

This way you have written includes all of your a tag, and if you have hyperlink in your menus your menus will do thus function too, at first give an id to your hyperlink 这样编写的方式将包含所有标签,并且如果菜单中具有超链接,则菜单也将因此起作用,首先给超链接添加一个ID

<a href="#" id="Something">Click On Me</a>

then use this in jquery: 然后在jquery中使用它:

$("#Something").click(function(e){
     e.preventDefault();
     //write your function here
});

this prevents page changing and will do your function. 这样可以防止页面更改,并可以完成您的功能。

You can call it from anything you want, but an anchor tag is preferred since if the user doesn't have JavaScript activated, you will have a fallback and can redirect the user to an other page. 您可以从任何想要的位置调用它,但是首选使用锚标记,因为如果用户未激活JavaScript,则将有一个后备功能,可以将用户重定向到其他页面。 Just be sure to prevent the default behaviour. 只要确保防止默认行为即可。

return false is not recommended for for that since it also prevent the event from bubbling. 为此,不建议return false ,因为它还可以防止事件冒泡。 You should use event.preventDefault() . 您应该使用event.preventDefault()

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

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