开发者

jQuery variable substitution

开发者 https://www.devze.com 2023-04-07 10:53 出处:网络
I have a simple jQuery function below.. I\'m interested in dynamically changing the \'query:FOOTBALL\' attribute with a dynamic page variable. So something like \'query:PAGE_TITLE\' or PAGE_VAR

I have a simple jQuery function below.. I'm interested in dynamically changing the 'query:FOOTBALL' attribute with a dynamic page variable. So something like 'query:PAGE_TITLE' or PAGE_VAR

I haven't had any luck get .replace() to work.. I must be overlooking something very simp开发者_C百科le; What approach do you all take here?

  jQuery(function($){
      $("#query").tweet({
        avatar_size: 32,
        count: 4,
        query: "FOOTBALL",
        loading_text: "searching twitter..."
      });
  });


I'm not exactly clear on what you're asking, but I think you want to use a variable instead of a string literal... As query is expecting a String, you can use any String value there. So if you have a variable called myVar containing a string, you can use myVar in place of the string literal:

var myVar = "Some String";
jQuery(function($){
  $("#query").tweet({
    avatar_size: 32,
    count: 4,
    query: myVar,
    loading_text: "searching twitter..."
  });
});

Or, you could do something like this to use the value entered into an input by the user:

jQuery(function($){
  $("#query").tweet({
    avatar_size: 32,
    count: 4,
    query: $("#someInput").val(), //val() returns a string so it will work here
    loading_text: "searching twitter..."
  });
});


Just stuff it in a variable, then pass along. The page will have already loaded so you can grab the text from any element you'd like with jQuery.

jQuery(function($){
    // maybe you want the <title> tag value
    var query = location.title; 
    // or maybe the text from the first <h1>
    //query = $('h1:first').text(); 

    $("#query").tweet({
        avatar_size: 32,
        count: 4,
        query: query,
        loading_text: "searching twitter..."
    });
});
0

精彩评论

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