jQuery 코드를 사용하여 스팬 클래스 내부의 모든 것을 가져 와서 대신 내 제휴사에 대한 링크를 배치합니다.) } }데이터 매개 변수를 사용하여 jquery 코드를 HTML 5와 호환되도록 수정하십시오.
내가 수정하는 방법;
// Function for rendering affiliate links in a way that won't pass PageRank.
function affiliateLinks(){
// Declare local variables.
var theURL, theAnchorText, theTitle;
// Declare the variable "spans" as the collection of all <span> elements.
var spans = document.getElementsByTagName('span');
// Perform the following steps for every <span> element.
for (var i = 0; i<spans.length; i++){
// If the <span> element is part of the "affiliate" class...
if (hasClass(spans[i], 'affiliate')){
// Use the content between the <span> tags as our affiliate link's anchor text.
// Example: <span class="affiliate" title="Affiliate Site">this will be our anchor text</span>
// The content doesn't have to be just text; it can be HTML too.
// Example: <span class="affiliate" title="Affiliate Site"><img src="/banners/affiliate-logo.png" /></span>
theAnchorText = spans[i].innerHTML;
// Get the value of the <span> element's title attribute, make it lowercase, and remove whitespace
// characters from the beginning and end.
theTitle = spans[i].title;
campaign = spans[i].campaign;
theURL = 'http://www.test.com/dir/' + theTitle + '?psid=testcampaign_id=' + campaign + '';
// Insert the new affiliate link into its corresponding <span> element and copy the <span> element's
// CSS classes (all of them) into the affiliate link's <a> tag.
spans[i].innerHTML = '<a href="' + theURL + '" target="_blank" class="' + spans[i].className + '">' + theAnchorText + '</a>';
// Remove the title attribute from the <span> element, to prevent Firefox from displaying it in a tooltip.
spans[i].removeAttribute('title');
}
감사합니다 : 이것은 JQuery와는
<span class="affiliate" title="mytitle" campaign="mycampaign">Join</span>
:
이 스팬 클래스는 다음과 같습니다 여기에 코드입니다 내 jquery이 코드는 HTML 5를 사용하고 데이터 매개 변수를 사용하려는 경우. 나는 campaing과 title 대신 data-title과 data-campaign을 가질 것인가?
<span class="affiliate" data-title="mytitle" data-campaign="mycampaign">Join</span>
내 JQuery와에이 일을 시도했지만 작동하지 않습니다
theTitle = spans[i].data-title;
campaign = spans[i].data-campaign;
당신에게
당신은 그런 속성 이름으로data-title
을 넣을 수 없습니다
JavaScript와 jQuery의 차이점을 알고 계십니까? – Skilldrick
현재 이것은 jQuery를 전혀 사용하지 않고 자바 스크립트입니다. –