1

该脚本可以正常工作,正确创建所有内容等,但是 jquery addClass 方法没有添加该类。我是在 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

由于modalelement是 ID,您应该更正选择器以将它们用作 ID:

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

边注。使用 jQuery 找到 DOM 元素后,无需使用以下命令执行第二次搜索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 回答