카테고리 없음

[자바 스크립트] jQuery : .ready () 중에 문서 제목을 변경하는 방법은 무엇입니까?

필살기쓰세요 2020. 12. 10. 15:00

다음은 작동하지만 SEO와 호환되지 않습니다. 제목 태그에 제목을 넣는 것이 가장 좋습니다.

<script type="text/javascript">

    $(document).ready(function() {
            document.title = 'blah';
                });
                
                </script>
                
-------------------

$('title').text('hi')IE에서 지원하지 않으므로을 사용하지 마십시오 .

사용하는 것이 좋습니다 document.title = 'new title';

-------------------

이것은 모든 브라우저에서 잘 작동합니다 ...

$(document).attr("title", "New Title");

IE에서도 작동

-------------------

이렇게 :

$(document).ready(function ()
{
    document.title = "Hello World!";
    });
    

검색 엔진에서 사이트의 색인을 올바르게 생성하려면 기본 제목을 설정해야합니다.

약간의 팁 :

$(function ()
{
    // this is a shorthand for the whole document-ready thing
        // In my opinion, it's more readable 
        });
        
-------------------
<script type="text/javascript">
$(document).ready(function() {

    $(this).attr("title", "sometitle");
    
    });
    </script>
    
-------------------

document.title이 작동하지 않았습니다.

다음은 JQuery를 사용하여 수행하는 또 다른 방법입니다.

$('html head').find('title').text("My New Page Title");
-------------------

Ruby on Rails에서 일부 중첩 레이아웃을 사용하고 있으며 레이아웃 중 하나에서 div의 문자열을 읽고 문서 제목으로 설정해야합니다.

이를 수행하는 올바른 방법은 서버 측에 있습니다.

레이아웃 에는 텍스트를 div에 넣는 일부 코드가 있습니다 . 이 코드는 또한와 같은 인스턴스 변수를 설정하고 @page_title외부 레이아웃에서<%= @page_title || 'Default Title' %>

-------------------

현재 제목 세션을 반영하는 서버 측 스크립트 get_title.php가 있으면 jQuery에서 잘 작동합니다.

$.get('get_title.php',function(*respons*){
    title=*respons* + 'whatever you want'   
        $(document).attr('title',title)
        })
        


출처
https://stackoverflow.com/questions/180153