2011-12-19 5 views
0

JavaScript를 사용하여 표시 할 텍스트를 얻으려고하는데 작동하지 않는 것 같아서 그 이유를 완전히 알지 못합니다.JavaScript를 사용하여 텍스트 삽입

마치 집어 들고있는 것 같지만 실제로는 어디에도 보이지 않습니다.

<script type="text/javascript"> 
jQuery('.set1').each(function() { 
    jQuery(this).before(jQuery('<img>').text("Some Text put here with Javascript... ")); 
}); 
</script> 

<div class="image_carousel"> 
    <div id="foo1"> 
<img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-db-1.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="679" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-db-2.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="679" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-db-3.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="692" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-7.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="339" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-8.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="338" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-db-4.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="691" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-11.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="339" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vision-12.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="339" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/smug-1.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="710" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/smug-2.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="710" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/smug-3.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="710" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/smug-4.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="710" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/smug-5.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="710" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/volt-4.png" class="set2" alt="Photography | Rory DCS" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/volt-1.png" class="set2" alt="Photography | Rory DCS" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/volt-7.png" class="set2" alt="Photography | Rory DCS" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/volt-9.png" class="set2" alt="Photography | Rory DCS" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/volt-3.png" alt="Photography | Rory DCS" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/volt-11.png" class="set2" alt="Photography | Rory DCS" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/hay-5.png" class="set3" alt="Photography | Alex Sainsbury" width="312" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/hay-4.png" class="set3" alt="Photography | Alex Sainsbury" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/hay-2.png" class="set3" alt="Photography | Alex Sainsbury" width="279" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/hay-3.png" class="set3" alt="Photography | Alex Sainsbury" width="292" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/hay-1.png" class="set3" alt="Photography | Alex Sainsbury" width="308" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/gana-1.png" class="set4" alt="Photography | Robert Charbonnet" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/gana-2.png" class="set4" alt="Photography | Robert Charbonnet" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/gana-3.png" class="set4" alt="Photography | Robert Charbonnet" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/gana-4.png" class="set4" alt="Photography | Robert Charbonnet" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/Peregrine-Shot_03-4.png" alt="Photography | David Goldman" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/Peregrine-Shot_09-84.png" alt="Photography | David Goldman" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/Peregrine-Shot_10-192.png" alt="Photography | David Goldman" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/Peregrine-Shot_16-48.png" alt="Photography | David Goldman" width="675" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/Peregrine-Shot_17-131.png" alt="Photography | David Goldman" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/ss2011-shot05-173.png" alt="Photography | David Goldman" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/ss2011-shot06-72-1.png" alt="Photography | David Goldman" width="300" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-db-1.png" alt="Photography | Chad Pickard and Paul McLean" width="672" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-3.png" alt="Photography | Chad Pickard and Paul McLean" width="343" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-4.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="342" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-db-2.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="671" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-7.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="298" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-8.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="299" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-db-3.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="694" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-db-4.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="685" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-13.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="341" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-14.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="343" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/09/vc-15.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="342" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/lurve-5.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="301" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/lurve-2.png" class="set1" alt="Photography | Chad Pickard and Paul McLean" width="673" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/lurve-3.png" class="set1"alt="Photography | Chad Pickard and Paul McLean" width="301" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/vampire-weekend.png" class="set2" alt="Photography | Alan Clarke" width="660" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/grizzle-bear.png" class="set2" alt="Photography | Alan Clarke" width="660" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/tg-5.png" class="set3" alt="Photography | Harley Weir" width="588" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/tg-11.png" class="set3" alt="Photography | Harley Weir" width="343" height="450" /><img src="http://www.dwmu.co.uk/wp-content/uploads/2011/07/tg-12.png" class="set3" alt="Photography | Harley Weir" width="341" height="450" /> 
    </div> 
    <div class="clearfix"></div> 
    <a class="prev" id="foo1_prev" href="#"><span>prev</span></a> 
    <a class="next" id="foo1_next" href="#"><span>next</span></a> 
    <div class="logbox" id="foo1_log"><p>Click the previous- or next-button</p></div> 
</div> 

어떤 아이디어 :

코드인가?

+1

의 고도 값을 사용하는 ...이다 너 뭘 하려구? – Jemaclus

+2

나는 이것의 CSS 부분을 놓친 유일한 사람인가? – Ryan

+0

@melihcelik : jQuery, CSS가 아닙니다. 태그 달기 및 편집하기. – Ryan

답변

2

:는이 같은 .before() 방법에 대한 권리 인수에 내용을 넣어? 내가 바로, 당신은 이미지 태그에 텍스트를 넣어 위해 노력하고 있음을 읽으면

$('.set1').each(function() { 
    $(this).before('<span>Some Text put here with Javascript... </span>'); 
}); 

http://jsfiddle.net/pixelass/nBVwz/ 또는 http://jsfiddle.net/pixelass/nBVwz/1/ 이미지

$('.set1').each(function() { 
    var text = $(this).attr('alt');//get the alt value 

    $(this).before('<span>' + text + '</span>');// put a span with the alt value before the image 
}); 
+0

내 대답을 업데이트하여 이미지 대체를 선택하고 이미지 앞에 표시하십시오. – pixelass

4

먼저, 문서로드가 완료되기 전에 스크립트가 실행 중입니다. DOM이 완전히로드되면 ready을 사용하여 콜백을 실행하십시오. 둘째, 텍스트를 어딘가에 배치해야합니다 (예 : 요소 내부에 없음). 이것은 하나 개의 옵션입니다 :

jQuery(document).ready(function() { 
    jQuery(".set1").each(function() { 
     jQuery(this).before("<p>Some Text put here with Javascript..."); 
    }); 
}); 

간단한 데모 : http://jsfiddle.net/cRRVX/

0

난 당신이 '고도'또는 '제목'attributes 의미 확신 : 등

jQuery(this).before(jQuery('<img>').attr("alt", "Some Text put here with Javascript... ")); 

2

1) 문서가로드 될 때까지 기다리고 2) 페이지에 표시하기 위해 span 또는 div와 같은 이미지 태그 이외의 텍스트에 텍스트를 넣어야하고 3) c 당신이 이미지 전에 텍스트를 넣을

jQuery(document).ready(function() { 
    jQuery('.set1').each(function() { 
     jQuery(this).before('<span>Some Text put here with Javascript... </span>'); 
    }); 
}); 
관련 문제