开发者

change font in jquery Not working

开发者 https://www.devze.com 2023-01-13 22:29 出处:网络
I have the following code i am trying to change the font of a peice of text but it keeps coming up with a page error.

I have the following code i am trying to change the font of a peice of text but it keeps coming up with a page error.

 function edit_font(div_id) {   
 $("#testsave").html(<strong onclick=\"javascript:save_font_stuff("+div_id+",font,'Arial')\">Done!</strong>
 } 

function save_font_stuff(div_id,font_setting,settings_vaule) {  
      if(font_setting=="font"){ 
          $("#"+div_id).css('font',settings_vaule);
      }else if(font_setting=="size"){
          $("#"+div_id).css('fontSize', settings_vaule);
      }else if(font_setting=="colour"){
          $("#"+div_id).css('color开发者_JAVA技巧',settings_vaule);
}
 } 


The syntax highlighting it pointing it out in this case, you have to pass a string to .html() (or at least in this case), so it needs quotes like this:

function edit_font(div_id) {   
  $("#testsave").html("<strong onclick=\"javascript:save_font_stuff("+div_id+",'font','Arial')\">Done!</strong>");
}

The missing quotes around the string in general and around the font variable in the call will both throw an error here. Have you considered using a non-inline event handler? For example:

function edit_font(div_id) {   
  $("#testsave").empty().append(
    $("<strong>Done!</strong>").click(function() { 
      save_font_stuff(div_id,'font','Arial');
    })
  );
}

You could also simplify the other function, if not just collapsing it, like this:

var setting_map = { font: 'font', size: 'fontSize', colour: 'color' };
function save_font_stuff(div_id, setting, vaule) { 
  $("#"+div_id).css(setting_map[setting], vaule);
} 


Your onclick event handler is not passing a string as its second argument. Try surrounding the font argument to a string.

function edit_font(div_id) {   
 $("#testsave").html('<strong onclick="javascript:save_font_stuff(\"'+div_id+'\",\'font\',\'Arial\')">Done!</strong>');
 } 
0

精彩评论

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