1

スクリプトは機能し、すべてを正しく作成するなどですが、jqueryaddClassメソッドはクラスを追加していません。私はjavascript関数でjqueryメソッドを使用するのは初めてですが、jqueryを使用せずに適切なクラスを追加するための代替メソッドを含め、助けていただければ幸いです。

function thumbnail(Img, Element, Modal, ModalTitle) {
"use strict";
/*jslint browser:true*/
/*global $, jQuery*/
//this function will append an anchor tag with the appropriate functionality, accepting inputs on the image filename, attaching element, and modal
//Img = full filename in format of foo.png
//Element = the ID of the element within which the thumbnail anchor will be placed
//Modal = the ID of the modal
//ModalTitle = Text used for title of the modal and title of the caption
var image, element, modal, loc, output, iUrl, modal_loc, modal_output, mtitle;
image = Img;
element = Element;
modal = Modal;
mtitle = ModalTitle;
iUrl = "/design-library/images/thumbs/" + image;
output = "<a href='#' data-reveal-id='" + modal + "'>";
output += "<img class='sample_img' src='" + iUrl + "' alt='" + mtitle + "' />";
output += "</a>";
output += "<p class='caption'>" + mtitle + "</p>";
modal_output = "<h1>" + mtitle + "</h1>";
modal_output += "<img src='" + iUrl + "' alt='" + image + "' /><a class='close-reveal-modal'>&#215;</a>";
//create the modal container
$(modal).addClass('reveal-modal');
modal_loc = document.getElementById(modal);
modal_loc.innerHTML = modal_output;
//the end of the script gets the element and adds the anchor tag that exists in output
$(element).addClass('samples');
loc = document.getElementById(element);
loc.innerHTML = output;
}
4

3 に答える 3

5

modalとはIDであるためelement、セレクターを修正して、セレクターを1つとして使用する必要があります。

$('#' + modal).addClass('reveal-modal');
$('#' + element).addClass('samples');

サイドノート。jQueryでDOM要素を見つけたら、次のコマンドで2番目の検索を実行する必要はありませんgetElementById

var modal_loc = $('#' + modal);
modal_loc.addClass('reveal-modal');
modal_loc.html(modal_output);
于 2012-12-13T14:13:42.547 に答える
2

modalがID文字列の場合、次のことを行う必要があります。

$('#'+modal).addClass('reveal-modal');
于 2012-12-13T14:13:43.577 に答える
1

変更してみてください:

$(element).addClass('samples');

$('#' + element).addClass('samples');
于 2012-12-13T14:13:31.860 に答える