0

我正在尝试为在线font预览目的设置小型 Web 应用程序。直到现在一切都很好。我已经制作了选择框(工作正常),测试文本区域(工作正常),字体大小(工作正常),粗体,斜体复选框......(工作正常),其中包含字体列表。当我从选择列表中选择一种字体时,页面上的所有内容(预定义文本)都已更改,这没关系,但我决定添加箭头(下一个和上一个)以进行快速更改,这也有效,但某些元素没有更改按钮单击。

实际问题是什么?

当我从选择列表中选择字体时,所有内容都会更改,但是当我将这些按钮用于下一个/上一个 H1 时,并且页面标题不想更改,因为我需要在页面标题以及页面标题上显示字体名称。

这里是:http: //jsfiddle.net/S8PwY/

有人可以检查此脚本并指出问题所在:

$(document).ready(function() {
$(".fontpicker").change(function() {
    $font = $('.fontpicker option:selected').val();
    $('.font_preview').css('fontFamily', $font);
});
$(".fontpicker").change(function() {
    $font = $('.fontpicker option:selected').val();
    $('.name').css('fontFamily', $font);
});



//min font size
var min = 10;

//max font size
var max = 60;

//grab the default font size
var reset = $('textarea').css('fontSize');

//font resize these elements
var elm = $('textarea');

//set the default font size and remove px from the value
var size = str_replace(reset, 'px', '');

//Increase font size
$('a.fontSizePlus').click(function() {

    //if the font size is lower or equal than the max value
    if (size <= max) {

        //increase the size
        size++;

        //set the font size
        elm.css({
            'fontSize': size
        });
    }

    //cancel a click event
    return false;

});

$('a.fontSizeMinus').click(function() {

    //if the font size is greater or equal than min value
    if (size >= min) {

        //decrease the size
        size--;

        //set the font size
        elm.css({
            'fontSize': size
        });
    }

    //cancel a click event
    return false;

});

//Reset the font size
$('a.fontReset').click(function() {

    //set the default font size    
    elm.css({
        'fontSize': reset
   }



$('.selector').click(function() {
    var checked = $(this).is(':checked');
    var value = $(this).attr('value');
    if (checked) {
        $('.font_preview, textarea').addClass(value);
    } else {
        $('.font_preview, textarea').removeClass(value);
    }
});

$("#next").click(function() {
    $("#mycars").val($("#mycars > option:selected").next().val());
    $font = $('.fontpicker option:selected').val();
    $('.font_preview').css('fontFamily', $font);
    $('.name').css('fontFamily', $font);

});

$("#prev").click(function() {
    $("#mycars").val($("#mycars > option:selected").prev().val());
    $font = $('.fontpicker option:selected').val();
    $('.font_preview').css('fontFamily', $font);
    $('.name').css('fontFamily', $font);
});

 $("select").change(function () {
      var str = "";
      $("select option:selected").each(function () {
            str += $(this).text() + " ";
          });
      $("title,h1.name,th.name").text(str);
    })
    .trigger('change'); 
4

2 回答 2

2

change您可以在每个事件中触发事件click以进行更改:

$("#next").click(function() {
    $("#mycars").val($("#mycars > option:selected").next().val()).trigger("change");
});

$("#prev").click(function() {
    $("#mycars").val($("#mycars > option:selected").prev().val()).trigger("change");
});

演示:http: //jsfiddle.net/u7dDw/

于 2012-05-05T11:34:18.350 回答
2

代码可以合并和固定如下:

$(".fontpicker").change(function() {
    $font = $('.fontpicker option:selected').val();
    $('.name').css('fontFamily', $font);
    $('.font_preview').css('fontFamily', $font);
    $("title,h1.name,th.name").text($font);
}).change();

$("#next").click(function() {
    $(".fontpicker").val($(".fontpicker >option:selected").next().val()).change();
});

$("#prev").click(function() {
    $(".fontpicker").val($(".fontpicker >option:selected").prev().val()).change();
});

查看更新的小提琴

于 2012-05-05T11:48:32.240 回答